Is there any way to raise the original exception that made the call to
__getattr__? I seem to stumble upon a problem where multi-layered attribute
failure gets obscured due to use of __getattr__. Here's a dummy code to
demonstrate my problems:
"""
import traceback


class BackupAlphabet(object):
    pass


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


    def __getattr__(self, name):
        if name == "b":
            return "banana"

        raise AttributeError(
            "'{0} object has no attribute '{1}'"
            .format(self.__class__.__name__, name))


alphabet = Alphabet()
backupalphabet = BackupAlphabet()

print(alphabet.a)
print(alphabet.b)
"""

Running the above code produces this:
"""
Traceback (most recent call last):
  File "example.py", line 26, in <module>
    print(alphabet.a)
  File "example.py", line 20, in __getattr__
    .format(self.__class__.__name__, name))
AttributeError: 'Alphabet object has no attribute 'a'
"""

While it's easy enough to identify the problem here, the traceback is
rather unhelpful in complex situations. Any comments?

Regards,
TB
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to