Python基类共享属性?

tad*_*tad 10 python

test.py中的代码:

class Base(object):
    def __init__(self, l=[]):
        self.l = l

    def add(self, num):
        self.l.append(num)

    def remove(self, num):
        self.l.remove(num)

class Derived(Base):
    def __init__(self, l=[]):
        super(Derived, self).__init__(l)
Run Code Online (Sandbox Code Playgroud)

Python shell会话:

Python 2.6.5 (r265:79063, Apr  1 2010, 05:22:20) 
[GCC 4.4.3 20100316 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> a = test.Derived()
>>> b = test.Derived()
>>> a.l
[]
>>> b.l
[]
>>> a.add(1)
>>> a.l
[1]
>>> b.l
[1]
>>> c = test.Derived()
>>> c.l
[1]
Run Code Online (Sandbox Code Playgroud)

我期待"类似C++"的行为,其中每个派生对象都包含自己的基类实例.这仍然是这样吗?为什么每个对象看起来共享相同的列表实例?

Xav*_* Ho 18

你是一个常见的Python新手错误.

请参阅我的答案: 如何在Python中声明实例变量的默认值?

简要说明,Python只解释一次类定义.这意味着在__init__()方法中声明的所有内容仅创建一次.或者,换句话说,您的[]列表默认参数只进行一次.

然后在每次创建新类时为同一实例self.l = l分配引用,因此您不期望这种行为.

Pythonic方式是这个(部分代码):

def __init__(self, arg=None):
    if arg is None:
        arg = []
    self.arg = arg
Run Code Online (Sandbox Code Playgroud)

此外,您应该考虑使用比l难以阅读的更好的命名约定,并且可能被误认为是1|.