Simple python oo issue

Posted by Alex K on Stack Overflow See other posts from Stack Overflow or by Alex K
Published on 2010-04-19T13:22:40Z Indexed on 2010/04/19 13:33 UTC
Read the original article Hit count: 752

Filed under:
|
|

Hello,

Have a look a this simple example. I don't quite understand why o1 prints "Hello Alex" twice. I would think that because of the default self.a is always reset to the empty list. Could someone explain to me what's the rationale here? Thank you so much.

class A(object):
        def __init__(self, a=[]):
            self.a = a

o = A()
o.a.append('Hello')
o.a.append('Alex')
print ' '.join(o.a)

# >> prints Hello Alex

o1 = A()
o1.a.append('Hello')
o1.a.append('Alex')
print ' '.join(o1.a)

# >> prints Hello Alex Hello Alex

© Stack Overflow or respective owner

Related posts about python

Related posts about mutable