Can anyone tell me why this code complains that there's no __setitem__
on container? I thought I only needed __getitem__
on the container to fetch the item and then __iadd__
to set the value, don't know why it's expecting __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__'