1 import sys
2
3 class dummy(object):
4 def __init__(self, val):
5 self.val = val
6
7 class myobj(object):
8 def __init__(self, resources):
9 self._resources = resources
10
11 class ext(myobj):
12 def __init__(self, resources=[]):
13 #myobj.__init__(self, resources)
14 self._resources = resources
15
16 one = ext()
17 one._resources.append(1)
18 two = ext()
19
20 print one._resources
21 print two._resources
22
23 sys.exit(0)
这将打印对分配给 和 对象的对象one._resources
的one
引用two
。我认为这two
将是一个空数组,因为如果在创建对象时未定义它,它显然是这样设置的。取消注释myobj.__init__(self, resources)
做同样的事情。使用super(ext, self).__init__(resources)
也做同样的事情。
我可以让它工作的唯一方法是如果我使用以下内容:
two = ext(dummy(2))
在创建对象以使其工作时,我不必手动设置默认值。或者也许我会。有什么想法吗?
我使用 Python 2.5 和 2.6 进行了尝试。