Python calling functions in init

Python call function in init python code example

If you track function entries/exists and timings, separate functions gives you finer granularity in the timing metrics. The core of Object Oriented programming, is the ability to re-use things — be they classes, methods or variables.

How to call specific some function right after calling __init__ in Python?

You can add additional «constructors» in Python by using class methods that instantiate an instance of the class and perform modifications to that instance

class Permit(object): def __init__(self): super(Permit, self).__init__() # init todo list self.todo = [1,2,3,4,5] @classmethod def with_mod(cls, position, value): obj = cls() obj.todo.insert(position, value) return obj foo = Permit() # Instance with the default todo list bar = Permit.with_mod(6, 3) # Instance where the todo list is then modified 

I don’t think I quite understood what you need — but for one thing metaclasses can help, if you can attach a custom metaclass to your Permit class:

The metaclass ‘ __call__ method is what is responsible for creating a new instance of a class: it will call __new__ on the class, which returns a new, unitialized instance, then call __init__ passing it the instance __new__ returned, and return that instance again (disregaring the return value of __init__ ).

If you want something to run after __init__ , just add this logic to a custom metaclass call `:

class PostInit(type): # deriving from type is what makes a metaclass def __call__(cls, *args, **kw): instance = super().__call__(*args, **kw) # "instance" now contains the new instance, after __init__ is run # code to retrieve custom configurations and apply to the instance: . return instance class Permit(metaclass=PostInit): . 

Python — Should __init__() call the parent class’s, In Python, calling the super-class’ __init__ is optional. If you call it, it is then also optional whether to use the super identifier, or whether to explicitly name the super class: object.__init__ (self) In case of object, calling the super method is not strictly necessary, since the super method is empty. Same for __del__.

Читайте также:  Php sql запросы выборка

Python — Why call methods during __init__()

Sometimes code is separated into functions for readability purposes.

If your object initialization requires three steps, then logically it would make sense to break it into three functions. The names of those functions could describe which portion of the initialization they handle.

Another reason you might see an «init» function called from the true __init__ is if that function restores the object to a fresh state; in that case, you might want to call the «clean» function from outside __init__ in, for example, an object pool.

You’ve also hinted at a third reason for the reuse in your own question: if a subclass needs to change the order in which portions of the initialization happen (or omit/replace some portions entirely!) this is not possible with a monolithic __init__ but easy if it’s split into separate parts.

A fourth and final reason is for profiling. If you track function entries/exists and timings, separate functions gives you finer granularity in the timing metrics.

Regardless, how you code is up to you — but the approach you question here does have merits.

Perhaps so that initUI can be called again.

Only one reason I know of, the same as most languages — usability. The core of Object Oriented programming, is the ability to re-use things — be they classes, methods or variables. By separating our different methods/functions, we can call them later on. Whether you ever will call them later. that’s debatable. I think it comes down to good programming practice.

Python Argparse Examples, 3 hours ago· Example no 1. An efficient In the next step, we will initialize a variable “parser”, and at the same time, we will call the argumentpraser() method. This function is related to the argparse module. also discussed what the Python argparse module is and why users will utilize it if they have to generate …

Injecting function call after __init__ with decorator

Based on this post and this answer, an alternative way to do this is through a custom metaclass. This would work as follows (tested in Python 2.7):

# define a new metaclass which overrides the "__call__" function class NewInitCaller(type): def __call__(cls, *args, **kwargs): """Called when you call MyNewClass() """ obj = type.__call__(cls, *args, **kwargs) obj.new_init() return obj # then create a new class with the __metaclass__ set as our custom metaclass class MyNewClass(object): __metaclass__ = NewInitCaller def __init__(self): print "Init class" def new_init(self): print "New init!!" # when you create an instance a = MyNewClass() >>> Init class >>> New init!! 
  1. when you call MyNewClass() it searches for the metaclass, finds that you have defined NewInitCaller
  2. The metaclass __call__ function is called.
  3. This function creates the MyNewClass instance using type ,
  4. The instance runs its own __init__ (printing «Init class»).
  5. The meta class then calls the new_init function of the instance.
Читайте также:  Python string formatting with format

Here is the solution for Python 3.x, based on this post’s accepted answer. Also see PEP 3115 for reference, I think the rationale is an interesting read.

Changes in the example above are shown with comments; the only real change is the way the metaclass is defined, all other are trivial 2to3 modifications.

# define a new metaclass which overrides the "__call__" function class NewInitCaller(type): def __call__(cls, *args, **kwargs): """Called when you call MyNewClass() """ obj = type.__call__(cls, *args, **kwargs) obj.new_init() return obj # then create a new class with the metaclass passed as an argument class MyNewClass(object, metaclass=NewInitCaller): # added argument # __metaclass__ = NewInitCaller this line is removed; would not have effect def __init__(self): print("Init class") # function, not command def new_init(self): print("New init!!") # function, not command # when you create an instance a = MyNewClass() >>> Init class >>> New init!! 

Here’s a generalized form of jake77’s example which implements __post_init__ on a non-dataclass. This enables a subclass’s configure() to be automatically invoked in correct sequence after the base & subclass __init__ s have completed.

# define a new metaclass which overrides the "__call__" function class PostInitCaller(type): def __call__(cls, *args, **kwargs): """Called when you call BaseClass() """ print(f".__call__(, )") obj = type.__call__(cls, *args, **kwargs) obj.__post_init__(*args, **kwargs) return obj # then create a new class with the metaclass passed as an argument class BaseClass(object, metaclass=PostInitCaller): def __init__(self, *args, **kwargs): print(f".__init__(, )") super().__init__() def __post_init__(self, *args, **kwargs): print(f".__post_init__(, )") self.configure(*args, **kwargs) def configure(self, *args, **kwargs): print(f".configure(, )") class SubClass(BaseClass): def __init__(self, *args, **kwargs): print(f".__init__(, )") super().__init__(*args, **kwargs) def configure(self, *args, **kwargs): print(f".configure(, )") super().configure(*args, **kwargs) # when you create an instance a = SubClass('a', b='b') 
PostInitCaller.__call__(('a',), ) SubClass.__init__(('a',), ) BaseClass.__init__(('a',), ) BaseClass.__post_init__(('a',), ) SubClass.configure(('a',), ) BaseClass.configure(('a',), ) 

Defining and Calling a Function within a Python Class, The function doesn’t have any inherent connection to the class; it just takes a number and does stuff to that number without any thought of anything about your class. Or, if you don’t want to “pollute the global namespace”, you can just define it as a local function within arithmetic. Then arithmetic can just call …

Читайте также:  Python csv writer separator

Pytest mock a function call in __init__ of a class

You should mock your function with the path where it’s been used and not it’s original path or location. In the example bellows it’s defined that the fun method in the object MyClass will be mocked and the returned value will be «this is mocked».

from external_package.module import sub_module from my_class import MyClass def test_my_class(mocker): mocker.patch.object(MyClass.submodule, 'fun', return_value="this is mocked") obj = MyClass(10, 20) assert obj.z == "this is mocked" 

Check this link for some good examples of how to mock objects in Python.

I was able to solve this by slightly changing the import statement in the source code,

from external_package.module import sub_module class MyClass: def __init__(self, x, y): self.x = x self.y = y self.z = sub_module.fun() def printer(self): print(f"X : , y : , z: ") 

Note that I’m importing submodule and calling method from the imported sub_module.

Now, in the test I used the pytest-mock fixture like this,

import external_package def test_my_class(mocker): method = mocker.patch("external_package.module.sub_module.fun") method.return_value = "this is mocked" obj = MyClass(10, 20) assert obj.z == "this is mocked" 

I’m not sure why this worked and not the earlier approach. Hope someone can clear that.

__init__ in Python, Output: A init called B init called. So, the parent class constructor is called first. But in Python, it is not compulsory that the parent class constructor will always be called first. The order in which the __init__ method is called for a parent or a child class can be modified. This can simply be done by calling the parent …

Источник

Оцените статью