Python and object/class attrs - what's going on?

Posted by digitala on Stack Overflow See other posts from Stack Overflow or by digitala
Published on 2010-06-15T12:49:45Z Indexed on 2010/06/15 12:52 UTC
Read the original article Hit count: 126

Filed under:

Can someone explain why Python does the following?

>>> class Foo(object):
...   bar = []
...
>>> a = Foo()
>>> b = Foo()
>>> a.bar.append(1)
>>> b.bar
[1]
>>> a.bar = 1
>>> a.bar
1
>>> b.bar
[1]
>>> a.bar = []
>>> a.bar
[]
>>> b.bar
[1]
>>> del a.bar
>>> a.bar
[1]

It's rather confusing!

© Stack Overflow or respective owner

Related posts about python