[Python-bugs-list] [ python-Bugs-472940 ] can't getattr() attribute shown by dir()

noreply@sourceforge.net noreply@sourceforge.net
Sun, 21 Oct 2001 18:20:48 -0700


Bugs item #472940, was opened at 2001-10-19 15:31
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=472940&group_id=5470

Category: Python Interpreter Core
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Jeremy Hylton (jhylton)
Assigned to: Guido van Rossum (gvanrossum)
Summary: can't getattr() attribute shown by dir()

Initial Comment:
I occasionally write introspective code that does
something like:

for attr in dir(obj):
    print attr, getattr(obj, attr)

I ran into a problem with it, though:

>>> ArithmeticError.__init__
<unbound method ArithmeticError.__init__>
>>> dir(ArithmeticError.__init__)
['__call__', '__class__', '__cmp__', '__delattr__',
'__dict__', '__doc__', '__get__', '__getattribute__',
'__hash__', '__init__', '__name__', '__new__',
'__reduce__', '__repr__', '__setattr__', '__str__',
'im_class', 'im_func', 'im_self']
>>> '__dict__' in dir(ArithmeticError.__init__)
1
>>> ArithmeticError.__init__.__dict__
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'builtin_function_or_method' object has
no attribute '__dict__'



----------------------------------------------------------------------

>Comment By: Guido van Rossum (gvanrossum)
Date: 2001-10-21 18:20

Message:
Logged In: YES 
user_id=6380

Aha. No, I don't think such a hack is needed. We can simply
get rid of the instancemethod_getsetlist -- it defines
specific getset functions to get the __dict__, __doc__ and
__name__ of the underlying function, but that's not
necessary -- the normal mechanism that passes on getattr
requests already takes care of this. I'll check this in
shortly.

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2001-10-20 23:22

Message:
Logged In: YES 
user_id=31435

Guido, yes, the string '__dict__' is a key in

ArithmeticError.__init__.__class__.__dict__

so merge_class_dict() (object.c) merges it into the result 
dict for ArithmeticError.__init__.

Perhaps PyObject_Dir should try PyObject_HasAttr on every 
key it finds and weed out the ones that fail?  Surely a 
hack, but would fix Jeremy's problem. simply by trying it 
before he does <wink>.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-10-20 06:01

Message:
Logged In: YES 
user_id=6380

For Tim.

Is it possible that the __dict__ comes from looking at all
the attributes of foo.__class__?
ArithmeticError.__init__.__class__ has a __dict__ attribute.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=472940&group_id=5470