I see a misconception here between a constructor--constructing the object and initializing the object:
Python's use of __new__
and __init__
?
Use __new__
when you need to control the creation of a new instance. Use __init__
when you need to control initialization of a new instance.
So we must be careful here.
I read that the constructor is like the first argument passed to the class, which makes sense to me since the parameters seem to be passed to the class via the __init__
method.
The constructor is not passed to the class, to be precise the result of the constructor (__new__
) will be the first argument for every instance method in the class or its sub-classes (note: __new__
works only for new-style classes):
class A:
def __new__(self):
return 'xyz'
See what happens when you call the class (create the object):
>>> A()
'xyz'
>>> type(A())
<class 'str'>
Calling the class no longer return instance of type A
, because we changed the mechanism of the constructor __new__
. Actually by doing so you alter the whole meaning of your class, not only, this is pretty much hard to decipher. It's unlikely that you'll switch the type of object during the creating time of that specific object. I hope this sentence makes sense, if not, how will it make sense in your code!
class A:
def __new__(self):
return 'xyz'
def type_check(self):
print(type(self))
Look what happens when we try to call type_check
method:
>>> a = A()
>>> a
'xyz'
>>> a.type_check()
AttributeError: 'str' object has no attribute 'type_check'
a
is not an object of class A
, so basically you don't have access to class A
anymore.
__init__
is used to initialize the object's state. Instead of calling methods that will initialize the object's members after it's created, __init__
solves this issue by initializing the object's members during creation time, so if you have a member called name
inside a class and you want to initialize name
when you create the class instead of calling an extra method init_name('name')
, you would certainly use __init__
for this purpose.
So when I 'call' the class, I pass it the parameters from the __init__
method?
When you call the class, you pass the parameters (to) __init__
method?
Whatever arguments you pass the class, all the parameters will be passed to __init__
with one additional parameter added automatically for you which is the implied object usually called self
(the instance itself) that will be passed always as the left-most argument by Python automatically:
class A:
def __init__(self, a, b):
self.a = a
self.b = b
A( 34, 35)
self.a = 34 | |
| |
| | self.b = 35
init(self, a, b)
|
|
|
The instance that you created by calling the class A()
Note: __init__
works for both classic classes and new style classes. Whereas, __new__
works only for new-style classes.