[Python-bugs-list] [ python-Bugs-420304 ] getattr function w/ default
noreply@sourceforge.net
noreply@sourceforge.net
Thu, 03 May 2001 17:55:08 -0700
Bugs item #420304, was updated on 2001-04-30 17:14
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=420304&group_id=5470
Category: Python Interpreter Core
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Greg Chapman (glchapman)
Assigned to: Nobody/Anonymous (nobody)
Summary: getattr function w/ default
Initial Comment:
In Python 2.1, when calling the builtin getattr
function and supplying the (optional) default
argument, this default is returned on any exception
raised during PyObject_GetAttr, even if the exception
is not an AttributeError. I did not expect this
behavior after reading the getattr documentation. Is
this the intended behavior? (If so, I think the
documentation should be made clearer on this point.)
Example (providing a non-string as the attribute name):
>>> class Test:
... pass
...
>>> t = Test()
>>> getattr(t, 1, "hello")
'hello'
>>>
----------------------------------------------------------------------
Comment By: Chris Cogdon (chriscog)
Date: 2001-05-03 17:55
Message:
Logged In: YES
user_id=67116
Looks like an attribute error to me. consider this:
>>> getattr (t,fred,"thingy")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: There is no variable named 'fred'
Ie, in your own example, it couldn't find an attribute '1'
in the class, even if its an integer attribute (which is
only obtainable using getattr/setattr). That's exactly what
AttributeError is
----------------------------------------------------------------------
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=420304&group_id=5470