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__'
                Basically,
con['foo'] += "what's going on?"
compiles to something like:
item = con['foo']
item += "what's going on?"
conf['foo'] = item
You can see, decompiling the code, something like:
  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        
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With