A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2005-February/051775.html below:

[Python-Dev] Re: Confusing "hasattr" behaviour

[Python-Dev] Re: Confusing "hasattr" behaviour [Python-Dev] Re: Confusing "hasattr" behaviourTerry Reedy tjreedy at udel.edu
Thu Feb 24 22:25:07 CET 2005
"J. David Ibanez" <jdavid at itaapy.com> wrote in message 
news:421B8C4B.5050003 at 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



More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4