Class/Global variables vs Instance Variables ...

Class/Global variables are like CoW, only when the instances assign them values they have their own memory location allocated. For example in this case a.bar is assigned a value and hence it has different memory  location allocated, but the same is not the case with b.bar

>>> class Foo(object): 
        bar = None
>>> repr(Foo.bar)
'None'
>>> a = Foo()
>>> a.bar = 1
 >>> repr(Foo.bar)
'None'
>>> a.bar
1
>>> b = Foo()
>>> b.bar
>>> repr(b.bar)
'None'
>>> a.bar
1
>>> id(Foo.bar)
135796184
>>> id(a.bar)
151257264
>>> id(b.bar)
135796184

Comments

Popular posts from this blog

Multiple repeat error when the regex has multiple wildcards in python re module

Weakref proxy is for instance only ...

To speed up accessing files on nfs shares on a ubuntu machine ...