The following code
class Myclass():
print("from global")
a = 1
def __init__(self):
print("from init")
self.b = 2
x = Myclass()
print(x.a, x.b)
outputs
from global
from init
(1, 2)
What is the advantage of placing code which is executed when an object is created in __init__
(print("from init")
in the example above) instead of directly at the class level (print("from global")
)?