Junior — Middle
How is a new class object initialized in Python?
sobes.tech AI
Answer from AI
In Python, a new object of a class is created by calling the class as a function, which invokes the __new__ method to allocate memory and then __init__ to initialize the object.
Example:
class MyClass:
def __init__(self, value):
self.value = value
obj = MyClass(10) # Creates a new object of class MyClass with parameter 10
print(obj.value) # Will output: 10
Thus, to create a new object, it is enough to call the class with the required arguments, and Python will automatically invoke the __init__ constructor.