[issue30352] The 'in' syntax should work with any object that implements __iter__

Steven D'Aprano report at bugs.python.org
Fri May 12 10:39:13 EDT 2017


Steven D'Aprano added the comment:

A further thought... looking at your example code, I believe that part of the __getattr__ is redundant.

    def __getattr__(self, item):
        try:
            return self.__getattribute__(item)
        except AttributeError:
            return self.f.__getattribute__(item)


__getattr__ is only called if normal attribute lookup has already failed, so the call to self.__getattribute__ is unnecessary. If it would have succeeded, it would have already succeeded and __getattr__ won't have been called at all.

For more discussion on how to do automatic delegation, you should look at Alex Martelli's recipe from the Python Cookbook:

https://code.activestate.com/recipes/52295-automatic-delegation-as-an-alternative-to-inherita/

Also, its a bit... funny... to call dunder methods directly. (I'm deliberately not using the word "wrong".) They are implementation, not interface. I think your __getattr__ should be:

    def __getattr__(self, name):
        return getattr(self.f, name)

It also looks nicer :-)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue30352>
_______________________________________


More information about the Python-bugs-list mailing list