Junior — Middle
What happens first: the constructor execution when creating an object or calling the init method?
sobes.tech AI
Answer from AI
In Python, when creating an object, the __new__ method is called first, which is responsible for creating an instance of the class, then the constructor __init__ is called, which initializes the created object.
There is no separate standard method called init in Python, it is likely referring to __init__.
The order is:
__new__— creating the object__init__— initializing the object
Example:
class MyClass:
def __new__(cls, *args, **kwargs):
print('Creating object')
return super().__new__(cls)
def __init__(self):
print('Initializing object')
obj = MyClass()
Output:
Creating object
Initializing object