[Python-Dev] __getattribute__'s error is not available in __getattr__
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