7

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(object):
    def __init__(self):
        self._data = range(10)
        self.__getitem__ = self._data.__getitem__

However, I receive a

TypeError: 'wrap' object does not support indexing

for the print w[2] line with the latter version.

The direct call to the method, i.e., print w.__getitem__(2), works in both cases...

Why does the assignment version not allow indexing?


EDIT: Regarding the "closed for duplication"

I agree that the linked question has the same answer. It is, however, not at all clear that they are the same question. In particular, someone who does not know the answer here, also does not know that there is an overarching "type of problem" at work. Thus, it won't be clear that they find the answer in a seemingly unrelated question about __call__.

NichtJens
  • 1,709
  • 19
  • 27
  • Pro tip: If you didn't know why this breaks, you shouldn't try optimizing it. Its overhead is probably not as significant as you think it is. – MisterMiyagi Jun 30 '16 at 21:07
  • 1
    But, If I hadn't tried, I wouldn't have learned how it works, which wouldn't have allowed me to do it. OTOH, if I had known it breaks, I'd not have tried it ... Also, you probably don't know, how significant I thought it to be. – NichtJens Jun 30 '16 at 21:26
  • 1
    Note that you *can* actually solve this by creating a new class for every type. Basically you need a factory function for this. Which can be transparently implemented via `__new__`. It's some black magic, though... Of course I can just speculate how significant you think it its; for reference, you can safe roughly 0.1 usec per call. – MisterMiyagi Jun 30 '16 at 21:34
  • 1
    Care to write down some example code for that? And, please excuse my snarky comment, I meant to demonstrate that you are implying that learning how something works by breaking it is not a good strategy (which I think it is!) ... – NichtJens Jun 30 '16 at 21:46
  • 2
    To be quite honest with you, I didn't know this was possible either, before your snark motivated me. See my answer below on how to do it (and why it's actually a bad idea for other reasons ^^). Keep up the good snark and never stop breaking things. ;) – MisterMiyagi Jun 30 '16 at 21:54

1 Answers1

6

Special methods (essentially anything with two underscores on each end) have to be defined on the class. The internal lookup procedure for special methods completely skips the instance dict. Among other things, this is so if you do

class Foo(object):
    def __repr__(self):
        return 'Foo()'

the __repr__ method you defined is only used for instances of Foo, and not for repr(Foo).

NichtJens
  • 1,709
  • 19
  • 27
user2357112
  • 260,549
  • 28
  • 431
  • 505
  • Ah! I see. Could you give a link to the documentation for this? And, I don't really see the connection between your first paragraph (which answers my question) and your example... – NichtJens Jun 30 '16 at 21:09
  • 1
    @NichtJens: If special method lookup looked at the instance dict, then `repr(Foo)` would pick up the `Foo.__repr__` method you defined and use that. That's not what you want, because that method is only supposed to handle instances of `Foo`, not `Foo` itself. – user2357112 Jun 30 '16 at 21:10
  • @NichtJens: [Here's](https://docs.python.org/3/reference/datamodel.html#special-lookup) the documentation, which uses a similar example. – user2357112 Jun 30 '16 at 21:12
  • Great... Solved in ~5minutes. Thanks! – NichtJens Jun 30 '16 at 21:16