Junior — Middle
At what stage in the execution process does the interpreter set initial values for a class in Python?
sobes.tech AI
Answer from AI
In Python, initial values for a class are set during the execution of the class body, that is, at the time of its definition (when the interpreter reads and executes the class code).
At this stage, a class object (type) is created, and all class attributes and methods are initialized. After that, the class becomes available for creating instances.
Example:
class MyClass:
x = 10 # class attribute is initialized immediately upon definition
def __init__(self):
self.y = 20 # instance attribute is initialized when the object is created
Here, x gets its value immediately upon class definition, and y upon instance creation.