[Python-Dev] getattribute's error is not available in getattr (original) (raw)

Nick Coghlan ncoghlan at gmail.com
Tue May 2 00:44:56 EDT 2017


On 2 May 2017 at 13:47, Jason Maldonis <jjmaldonis at gmail.com> wrote:

Hi everyone,

If this should be asked in learn python I apologize -- please just tell me without answering. I'm working on a large class architecture and I find myself often overloading getattr. I am continuously running into the issue where I want getattr to have access to the error that was raised in getattribute, but it seems completely unavailable. Is that true?

getattr can be called from getattribute, so when it runs, getattribute hasn't necessarily failed yet - it may just be on its last resort strategy for attribute retrieval.

If you're sure the base class getattribute doesn't call getattr directly, you can do:

def __getattribute__(self, name):
    try:
        return super().__getattribute__(name)
    except AttributeError:
        return self.__getattr__(name)

However, would you mind filing a documentation bug for this? I can't find anything in the language or library reference that explicitly states whether or not object.__getattribute__ itself calls __getattr__ directly, and that's a docs limitation which should be addressed.

Cheers, Nick.

-- Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia



More information about the Python-Dev mailing list