Here is my code:
class MyClass:
def __init__(self):
self.value = 0
def set_value(self, value):
self.value = 5
def get_value(self):
return self.value
value = print("Hello")
a = MyClass()
The output is:
Hello
What I do not understand is why print("Hello")
gets executed. When I create an instance of the class only the instance variable is set to 0. Why self.value = 0
calls value = print("Hello")
?
Can someone explain me this behavior?
The code evaluates the class when you execute it, and calls the
print
to define the class variablevalue
.The below example shows that it's printed before the instanciation.