On Fri, 03 May 2013 05:34:40 +0600, Mr. Joe wrote:

> Is there any way to raise the original exception that made the call to
> __getattr__? 

No. There is some discussion on the Python-Dev mailing list about adding 
better error reporting to AttributeError, but that may not go anywhere, 
and even if it does, it won't help you until you have dropped all support 
for versions below Python 3.4 or 3.5.



> class BackupAlphabet(object):
>     pass
> 
> 
> class Alphabet(object):
>     @property
>     def a(self):
>         return backupalphabet.a

This raises AttributeError. Since __getattr__ uses AttributeError to 
decide whether or not the name exists, the behaviour shown is correct: 
Alphabet.a does not exist, as far as the Python semantics of attribute 
access are concerned.

Either write better code *wink* that doesn't raise AttributeError from 
inside properties, or wrap them with something like this:

class Alphabet(object):
    @property
    def a(self):
        try:
            return backupalphabet.a
        except AttributeError:
            raise MyCustomError

where MyCustomError does NOT inherit from AttributeError.

If you're doing this a lot, you can create a decorator to do the wrapping.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to