Middle+
Does __init__ create an object?
sobes.tech AI
Answer from AI
The __init__ method in Python does not create an object; it initializes an already created object.
When you call a class, for example obj = MyClass(), first the __new__ method is called, which is responsible for creating a new instance of the class (object). After the object is created, __init__ is called to initialize this object — setting initial attribute values and performing other setup.
Example:
class MyClass:
def __new__(cls, *args, **kwargs):
print("Creating object")
return super().__new__(cls)
def __init__(self, value):
print("Initializing object")
self.value = value
obj = MyClass(10)
The output will be:
Creating object
Initializing object
Thus, __init__ is not a constructor in the traditional sense, but a method for initializing an object after its creation.