By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,944 Members | 1,819 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,944 IT Pros & Developers. It's quick & easy.

Gotcha I never ran into before

P: n/a
I've been programming in Python for about 6 years now. One of the
features I adore the most is the very useful error messages and stack
traces that make it easy to debug. However, today I ran into a
difficult to trace bug because the stack trace was reporting the
problem in the wrong place.

class Delegator(object):
def __init__(self, obj):
self.obj = obj
def __getattr__(self, attr):
return getattr(self.obj, attr)

class SpecializedDelegator(Delegator):
def get_blah(self):
return ["Returning Blah"].upper()
blah = property(fget=get_blah)

print SpecializedDelegator("Doesn't Matter").blah

The stack trace is:
Traceback (most recent call last):
File "test.py", line 12, in ?
print SpecializedDelegator("Doesn't Matter").blah
File "test.py", line 5, in __getattr__
return getattr(self.obj, attr)
AttributeError: 'str' object has no attribute 'blah'

Which is correct, but says nothing about the real problem inside the
get_blah method. Is there a good reason that when a property's fget
function throws an AttributeError that it should fall back on
__getattr__? I would think since the attribute was explicitly defined
as a property the property function should be allowed to fully crash
and burn.

Note: This example is broken up into two classes because that is how I
discovered it. Since both classes were in separate files it added to
the agony of debugging this. Luckily I was making a small incremental
change so I could just back up and figure out what went wrong.

Thanks,
Brian
Aug 9 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.