[Python-Dev] Confusing "hasattr" behaviour

J. David Ibanez jdavid at itaapy.com
Tue Feb 22 20:47:23 CET 2005


  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 




More information about the Python-Dev mailing list