Assigning (instead of defining) a __getitem__ magic method breaks indexing [duplicate]
问题 This question already has answers here : Why won't dynamically adding a `__call__` method to an instance work? (2 answers) Closed 11 months ago . I have a wrapper class similar to this (strongly simplified) example: class wrap(object): def __init__(self): self._data = range(10) def __getitem__(self, key): return self._data.__getitem__(key) I can use it like this: w = wrap() print w[2] # yields \"2\" I thought I could optimize and get rid of one function call by changing to this: class wrap