What are the steps that the interpreter takes when creating an instance of the object?

I would like to know the order of all the actions taken by the interpreter behind the scenes (regardless of whether they are explicitly declared or not).

For instance:

  • __init__ call
  • super() call
  • __new__ call
  • global variables
  • initialization of constructor variables
  • decorator calls, etc.

EDIT: let's say this is a class that has everything (you name it)

 global_var = 2468 class A(superA): class_var = 1234 __init__(self): superA.__init__(self) another_var = 5678 @random_decorator def method(self): # do stuff 
+4
source share
2 answers
 class Foo(object): def __init__(self, *args, **kwargs): pass >>> foo = Foo(a, b=c) 
  • It calls type.__call__(Foo, a, b=c) ... (instead of type may be a metaclass of your class)
  • ... which call Foo.__new__(a, b=c) , which instantiate Foo ...
  • ... and call foo.__init__(a, b=c) and return Foo .

In other words, b = B(*args) works like type.__call__(B, *args) , where type.__call__ can be expressed as:

 class type(object): def __call__(cls, *args, **kwargs) obj = cls.__new__(*args, **kwargs) obj.__init__(*args, **kwargs) return obj 

The super function is executed exactly where you call it (like a regular function).

Ornamentation is performed when classes and methods are initialized, replacing them with wrappers.

Full example:

 def dec(func): print 'decorate', func.__name__ def wrapper(*args): print 'call wrapper of', func.__name__ return func(*args) return wrapper class A(object): def __new__(*args): print 'call A.__new__' return object.__new__(*args) def __init__(self, *args): print 'call A.__init__' class MyMetaclass(type): def __call__(mcls, *args): print 'call MyMetaclass.__call__' return super(MyMetaclass, mcls).__call__(*args) class B(A): __metaclass__ = MyMetaclass @dec def __new__(*args): print 'call B.__new__' return A.__new__(*args) @dec def __init__(self, *args): print 'call B.__init__' return super(B, self).__init__(*args) print 'start creating instance' b = B() print 'end creating instance' 

Result:

 decorate __new__ decorate __init__ start creating instance call MyMetaclass.__call__ call wrapper of __new__ call B.__new__ call A.__new__ call wrapper of __init__ call B.__init__ call A.__init__ end creating instance 
+4
source

All this is pretty straight forward. When the module is loaded, the code runs from top to bottom. Thus, global variables will be created as they appear in the module code. When a function is called, the code in it is executed from top to bottom.

Decorators are a bit of an exception to the fact that they are called after the function they decorate has been defined in order from top to bottom. Therefore, if you have:

 @hits @dies @rofls def watcher(): pass 

it is the same as:

 def watcher(): pass watcher = hits(dies(rofls(watcher))) 

That makes sense if you think every decorator wraps everything underneath.

When an object is created, __new__ is first called, then __init__ . super() is called if you have a call to it somewhere, like everything else ... "constructor variables" are simply initialized whenever you have a variable assignment in __init__ .

EDIT: look at your example:

 class A(superA): class_var = 1234 def __init__(self): superA.__init__(self) another_var = 5678 @random_decorator def method(self): # do stuff 

Instead, I will teach you how to fish.

 def random_decorator(f): print "random_decorator called" return f def init_var(varname, val): print '%s being initialized' % (varname,) return val class superA(object): def __new__(cls): print 'superA.__new__ called' return super(superA, cls).__new__(cls) def __init__(self): print "superA.__init__ called" class A(superA): class_var = init_var('class_var', 1234) def __new__(cls): print 'A.__new__ called' return super(A, cls).__new__(cls) def __init__(self): print 'A.__init__ called' superA.__init__(self) another_var = init_var('another_var', 5678) @random_decorator def method(self): print 'method called' # do stuff class_var2 = init_var('class_var2', 9012) A().method() 

See in which order the print statements appear. A.

+2
source

Source: https://habr.com/ru/post/1439173/


All Articles