Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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):
        self.name = '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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading