<div dir="ltr">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:<div>
"""<br><div><div>import traceback</div><div><br></div><div><br></div><div>class BackupAlphabet(object):</div><div> pass</div><div><br></div><div><br></div><div>class Alphabet(object):</div><div> @property</div>
<div> def a(self):</div><div> return backupalphabet.a</div><div><br></div><div><br></div><div> def __getattr__(self, name):</div><div> if name == "b":</div><div> return "banana"</div>
<div><br></div><div> raise AttributeError(</div><div> "'{0} object has no attribute '{1}'"</div><div> .format(self.__class__.__name__, name))</div><div><br></div><div><br>
</div><div>alphabet = Alphabet()</div><div>backupalphabet = BackupAlphabet()</div><div><br></div><div>print(alphabet.a)</div><div>print(alphabet.b)</div></div><div>"""</div></div><div><br></div><div style>Running the above code produces this:</div>
<div>"""<br></div><div><div>Traceback (most recent call last):</div><div> File "example.py", line 26, in <module></div><div> print(alphabet.a)</div><div> File "example.py", line 20, in __getattr__</div>
<div> .format(self.__class__.__name__, name))</div><div>AttributeError: 'Alphabet object has no attribute 'a'</div></div><div>"""</div><div><br></div><div style>While it's easy enough to identify the problem here, the traceback is rather unhelpful in complex situations. Any comments?</div>
<div style><br></div><div style>Regards,</div><div style>TB</div></div>