Python 2.4 (#1, Feb 22 2005, 20:15:07) [GCC 3.3.5 (Gentoo Linux 3.3.5-r1, ssp-3.3.2-3, pie-8.7.7.1)] on linux2 Type "help", "copyright", "credits" or "license" for more information.
class A(object): ... def get_x(self): ... there_is_a_bug_here ... x = property(get_x, None, None, '') ... a = A() getattr(a, 'x') Traceback (most recent call last): File "<stdin>", line 1, in ? File "<stdin>", line 3, in get_x NameError: global name 'there_is_a_bug_here' is not defined hasattr(a, 'x') False
Today I have spent a while to hunt down a couple of bugs in my application, because "hasattr" was catching the exceptions. I see there was a patch (#504714, three years ago) that addressed this, but it was rejected because much code would get broken. But, is it going to be considered for sometime in the future? 3.0 maybe? And, would it really break so much code? Wouldn't it instead bring to the day light many bugs that are already there (but failing in a more silent way)? Thanks, -- J. David Ibáñez Itaapy <http://www.itaapy.com> Tel +33 (0)1 42 23 67 45 9 rue Darwin, 75018 Paris Fax +33 (0)1 53 28 27 88
"J. David Ibanez" <jdavid@itaapy.com> wrote in message news:421B8C4B.5050003@itaapy.com... Given that the behavior of hasattr is clearly defined in Lib Manual 2.1 as equivalent to def hasattr(obj, name): try: getattr(obj, name) return True except: return False I am not sure what could be confusing about it. It is a simple getattr wrapper converting 'got something' to True and 'did not get anything' (raised an exception instead) to False. Users should know this so they don't wastefully write 'if hasattr(o,n): x = getattr(o,n)' [snip]
Today I have spent a while to hunt down a couple of bugs in my application, because "hasattr" was catching the exceptions. [snip
If you want a different behavior, you can write your own version now, without waiting for a future that may never come, that only converts AttributeError to False and that possibly collapses all others to one type of your choosing. To make sure that even this does not mask a bug, one needs to make sure that .__getattr__ methods do not pass on unintended bug-indicating AttributeErrors. Terry J. Reedy
Terry Reedy wrote:
"J. David Ibanez" <jdavid@itaapy.com> wrote in message news:421B8C4B.5050003@itaapy.com...
Given that the behavior of hasattr is clearly defined in Lib Manual 2.1 as equivalent to
def hasattr(obj, name): try: getattr(obj, name) return True except: return False
I am not sure what could be confusing about it. It is a simple getattr wrapper converting 'got something' to True and 'did not get anything' (raised an exception instead) to False. Users should know this so they don't wastefully write 'if hasattr(o,n): x = getattr(o,n)'
See also http://python.org/sf/504714 Just
participants (3)
-
J. David Ibanez
-
Just van Rossum
-
Terry Reedy