When initializing an instance with python, it is often described in a special method called __init __ ()
, but the instance is not created here. While confirming that fact, let's find out where the process of creating an instance is written.
** Execution environment **
Name | Version |
---|---|
python | 3.4.3 |
ipython | 3.1.0 |
class Foo():
def __init__(self):
print('init')
print('self = %s' % self)
Foo()
# =>
# init
# self = <__main__.Foo object at 0x10dacb9b0>
Writing Class () will call the special method __init__
of that class. This is an instance method. The reason is that it takes an argument of self, that is, an instance. The variable name self is not grammatically fixed, so you can use another name, but it seems that it is customarily used as self.
So where is this instance created? First, let's suppress new that is executed before __init __ ().
[Official Document](http://docs.python.jp/3.4/reference/datamodel.html?highlight=%E7%89%B9%E6%AE%8A%E3%83%A1%E3%82%BD% E3% 83% 83% E3% 83% 89 # specialnames) states that __new__ ()
will be called to create a new instance. And this __new__ ()
is automatically defined at the time of class creation even if it is not specified.
class Foo():
def __init__(self):
pass
foo = Foo()
`__new__' in dir(foo)
# => True
You can also see in the official documentation that this __new __ ()
is executed before the __init __ ()
.
If new () returns an instance of cls, then the instance init () is called like init (self [, ...]). At this time, self is the newly created instance, and the remaining arguments are the same as those passed to new (). 3. Data model — Python 3.4.3 Documentation </ cite>
This can be confirmed in the source code below.
class Foo():
def __new__(cls, var1):
print('new')
print('var1 = %s' % var1)
return super().__new__(cls)
def __init__(self, var1):
print('init')
print('self = %s' % self)
print('var1 = %s' % var1)
Foo('my_value')
# =>
# new
# var1 = my_value
# init
# self = <test.Foo object at 0x103bd0208>
# var1 = my_value * Up to here is print()It is the output in.
#<test.Foo at 0x103bd0208>
super () calls the parent class. This time, the instance creation is left to the parent class. Since it overrides __new__ ()
, I used super (). __ new__ (cls)
to write the process of explicitly instantiating myself.
Even if you do not override __new__ ()
, if you leave the instance creation to the parent class __new__ ()
, the real instance creation process is written in the __new__ ()
of all parent classes object. It will be done. Alternatively, the instantiation process may be written in __new__ ()
of each class.
I still didn't know where the instantiation process was written. However, I found that ** Class () was calling __new__
**.
To confirm that the processing of this __new__ ()
is written, you can find it by executing it without parentheses () in the method name.
class Super():
@classmethod
def hello(cls):
print('super hello')
@classmethod
def say(cls):
print('super say')
class Sub(Super):
@classmethod
def hello(cls):
print('super hello')
#Check the method you defined and the class where it is defined
Super.hello
# => <bound method type.hello of <class 'test.Super'> >
Sub.hello
# => <bound method type.hello of <class 'test.Sub'> >
#If not overridden
Sub.say
# => <bound method type.say of <class 'test.Sub'> >
Super.__new__
# => <function object.__new__>
Super.__init__
# => <slot wrapper '__init__' of 'object' objects>
Super.__le__
# => <slot wrapper '__le__' of 'object' objects>
Not only __new__
, but other special methods themselves are defined by object. It turns out that all objects are left to object to instantiate unless overridden.
Recommended Posts