有谁能告诉我为什么这段代码会抱怨容器上没有__setitem__
?我以为我只需要容器上的__getitem__
来获取该项目,然后__iadd__
来设置该值,不知道为什么它会期待__setitem__
class Item:
def __init__(self):
pass
def __iadd__(self, value):
print 'added: ' + value
return self
class Container:
def __init__(self):
self.__items = {
'foo': Item()
}
def __getitem__(self, name):
return self.__items[name]
if __name__ == '__main__':
# works!
con = Container()
item = con['foo']
item += 'works!'
# wtf?
con['foo'] += "what's going on?"
# output:
# added: works!
# added: what's going on?
# Traceback (most recent call last):
# File "foo.py", line 27, in <module>
# con['foo'] += "what's going on?"
# AttributeError: Container instance has no attribute '__setitem__'
答案 0 :(得分:3)
让我这样说:如果你对+=
这样的不可变类型使用int
会怎么样?你能明白为什么在+=
返回的值上调用[]
还需要调用__setitem__
吗?
真正的诀窍是理解python中有=
的任何运算符都涉及赋值,因为它需要能够以预期的方式处理不可变类型。
为清楚起见,请考虑以下事项:
>>> a = 10*10**9
>>> b = a
>>> a is b
True
>>> a += 1
>>> a is b
False
显然,a
被分配用于引用新值。因此,当您对+=
返回的值执行__getitem__
时,会发生同样的情况。
不幸的是,python文档非常糟糕,说明这将会发生什么,但正如另一个答案所指出的那样,它的反编译很明显,并且必须正确处理存储在可变容器中的不可变类型。
答案 1 :(得分:2)
基本上,
con['foo'] += "what's going on?"
编译为:
item = con['foo']
item += "what's going on?"
conf['foo'] = item
你可以看到,反编译代码,如:
2 0 LOAD_GLOBAL 0 (con)
3 LOAD_CONST 1 ('foo')
6 DUP_TOPX 2
9 BINARY_SUBSCR
10 LOAD_CONST 2 ("what's going on?")
13 INPLACE_ADD
14 ROT_THREE
15 STORE_SUBSCR