from __future__ import print_function class Item(object): def __init__(self, value): self.value = value def __str__(self): return "Item " + str(self.value) def __iadd__(self, other): print("Item iadd", self, other) return Item(self.value + "+" + other.value) class Container(object): def __getitem__(self, key): print("Container getitem", key) return Item(key) def __setitem__(self, key, value): print("Container setitem", key, value) return self def __additem__(self, key, value): print("Container additem would be nice, but this is never called", key, value) return self c = Container() _ = c['key'] c['key'] = Item('other') print("---") c['key'] += Item('other')
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question