This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author steven.daprano
Recipients Edouard KLEIN, rhettinger, steven.daprano, xiang.zhang
Date 2017-05-12.14:39:13
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1494599953.5.0.613299899169.issue30352@psf.upfronthosting.co.za>
In-reply-to
Content
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 :-)
History
Date User Action Args
2017-05-12 14:39:13steven.dapranosetrecipients: + steven.daprano, rhettinger, xiang.zhang, Edouard KLEIN
2017-05-12 14:39:13steven.dapranosetmessageid: <1494599953.5.0.613299899169.issue30352@psf.upfronthosting.co.za>
2017-05-12 14:39:13steven.dapranolinkissue30352 messages
2017-05-12 14:39:13steven.dapranocreate