[Python-bugs-list] [ python-Bugs-718532 ] inspect, class instances and __getattr__

SourceForge.net noreply@sourceforge.net
Tue, 15 Apr 2003 03:01:29 -0700


Bugs item #718532, was opened at 2003-04-09 22:01
Message generated for change (Comment added) made by doerwalter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=718532&group_id=5470

Category: Python Library
Group: Python 2.2.2
Status: Open
Resolution: None
Priority: 5
Submitted By: Stefan Schwarzer (sschwarzer)
Assigned to: Nobody/Anonymous (nobody)
Summary: inspect, class instances and __getattr__

Initial Comment:
inspect.isclass(class_instance) fails if the
corresponding class uses a "wildcard" implementation of
__getattr__.

Example:

Python 2.2.2 (#1, Nov 13 2002, 22:53:57) 
[GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
Type "help", "copyright", "credits" or "license" for
more information.
>>> import inspect
>>> class X:
...     def __getattr__(self, name):
...             if name == 'foo':
...                     return 1   
...             if name == 'bar':
...                     return 2
...             else:
...                     return "default"
... 
>>> x = X()
>>> inspect.isclass(x)
1

The problematic expression in inspect.isclass is
hasattr(object, '__bases__') which returns a true value.

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

>Comment By: Walter Dörwald (doerwalter)
Date: 2003-04-15 12:01

Message:
Logged In: YES 
user_id=89016

type(object) in (types.TypeType, types.ClassType)
won't work with custom metaclasses.
isinstance(object, (type, types.ClassType))
would be better.


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

Comment By: Stefan Schwarzer (sschwarzer)
Date: 2003-04-15 10:01

Message:
Logged In: YES 
user_id=383516

Hello Raymond, thanks for your reply. In fact, I'm also not
sure if it counts as a bug. I also suggested a patch (handle
__getattr__ requests for __bases__ with an AttributeError)
for for the SF project which causes/d the problem.

I think, if there's a better way to decide on "class-ness"
than now, the code in inspect should be changed.
Fortunately, it doesn't have to be backward-compatible,
because the module is always distributed with a certain
interpreter version.

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

Comment By: Raymond Hettinger (rhettinger)
Date: 2003-04-15 02:36

Message:
Logged In: YES 
user_id=80475

Hmm.  I'm not sure that counts as a bug.  In an OO 
language, it's a feature that objects can be made to look 
like and be substituable for other types.  In this case, 
you've taught your object to be able to fake some classlike 
behavior (having a __bases__ attribute).

OTOH, inspect could have a stronger test for classhood:    
type(object) in (types.TypeType, types.ClassType)

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

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