Sobes.tech
Junior — Middle

What functions or procedures are activated during the initialization of a class object?

sobes.tech AI

Answer from AI

In Python, when initializing an object of a class, the __init__ method is automatically called. This method serves as a constructor and is used to set the initial values of the object's attributes.

Example:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

p = Person("Ivan", 30)
print(p.name)  # Outputs: Ivan

Besides __init__, when creating an object, the __new__ method is called first, which is responsible for creating the object itself, but most of the time, defining __init__ is sufficient for initialization.