How other members of a python class are bound to the instance?

I wrote codes as follow:

class a:
    e = [1,2,3]
    def __init__(self): = 'Adam'

b = a()
c = a()
if b.e is c.e:
    print('they are same')

the outcome is:

they are same

It shows that b.e and c.e point to the same object. And if I use id(a), it would return me an address in memory, showing that a has been created as an object.

When I want to creat an instance like b=a(), codes in the __init__() are excuted. But I am confused that when and how e (or other members like other methods) is bound to the instance? (Actually in __init__() there is no lines like "let’s bind e to this instance")

>Solution :

e in your case is a class variable because it is declared directly under your class and not in the init, this means that it is bound to the class and not to the instance of your object.

Leave a Reply