Debugging difficulty in python with __getattr__, decorated properties and AttributeError.
Mr. Joe
titanix88 at gmail.com
Thu May 2 19:34:40 EDT 2013
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130503/d6babb28/attachment.html>
More information about the Python-list
mailing list