What happens when I instantiate class in Python?

Posted by Konstantin on Stack Overflow See other posts from Stack Overflow or by Konstantin
Published on 2010-05-05T06:39:20Z Indexed on 2010/05/05 6:48 UTC
Read the original article Hit count: 182

Filed under:
|
|

Could you clarify some ideas behind Python classes and class instances?

Consider this:

class A():
    name = 'A'

a = A()

a.name = 'B' # point 1 (instance of class A is used here)

print a.name
print A.name

prints:

B
A

if instead in point 1 I use class name, output is different:

A.name = 'B' # point 1 (updated, class A itself is used here)

prints:

B
B

Even if classes in Python were some kind of prototype for class instances, I'd expect already created instances to remain intact, i.e. output like this:

A
B

Can you explain what is actually going on?

© Stack Overflow or respective owner

Related posts about python

Related posts about classes