Just because you initialize a new object, you do not call the code within its class.
You just call the __init__
. Method.
When Python first "reads" your code, it runs it all. And if it encounters a print()
statement, it will run it too. So, the python interpreter sees your code like this:
# It runs this line - Just a comment, so nothing to run
# A class deceleration, let us look at its body and then store it
class a:
# A print statement - I will run it. It does not add to the body, but I run all of the code I see
print('hello')
# This defines a method, so I will add it to the members of a
def __init__(self):
self.name = 'Adam'
# A new variable, I will call `__init__` - But just `__init__`
# I will not reparse the entire block, I will just jump to the location (Assuming JIT) where that function is stored and run it
b = a()
c = a()
So, we could rewrite your entire code like this:
print("hello")
class a:
def __init__():
pass
b = a()
b.name = "Adam"
c = a()
c.name = "Adam"