开发者

Simple python oo issue

开发者 https://www.devze.com 2022-12-27 04:05 出处:网络
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 expla

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


Read this Pitfall about mutable default function arguments: http://www.ferg.org/projects/python_gotchas.html

In short, when you define

def __init__(self,a=[])

The list referenced by self.a by default is defined only once, at definition-time, not run-time. So each time you call o.a.append or o1.a.append, you are modifying the same list.

The typical way to fix this is to say:

class A(object):
    def __init__(self, a=None):
        self.a = [] if a is None else a

By moving self.a=[] into the body of the __init__ function, a new empty list is created at run-time (each time __init__ is called), not at definition-time.


Default arguments in Python, like:

def blah(a="default value")

are evaluated once and re-used in every call, so when you modify a you modify a globally. A possible solution is to do:

def blah(a=None):
  if a is None
    a = []

You can read more about this issue on: http://www.ferg.org/projects/python_gotchas.html#contents_item_6

Basically, never use mutable objects, like lists or dictionaries on a default value for an argument.

0

精彩评论

暂无评论...
验证码 换一张
取 消