为什么python inspect.isclass认为实例是一个类?

时间:2021-07-30 16:49:17

Given the following module:

给出以下模块:

class Dummy(dict):
    def __init__(self, data):
        for key, value in data.iteritems():
            self.__setattr__(key, value)

    def __getattr__(self, attr):
        return self.get(attr, None)
    __setattr__=dict.__setitem__
    __delattr__=dict.__delitem__


foo=Dummy({"one":1, "two":2})

why does foo show up in the output of inspect.getmembers(..., predicate=inspect.isclass)?

为什么foo会出现在inspect.getmembers(...,predicate = inspect.isclass)的输出中?

$ python2.5
Python 2.5.2 (r252:60911, Aug 28 2008, 13:13:37) 
[GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import junk
>>> import inspect
>>> inspect.getmembers(junk, predicate=inspect.isclass)
[('Dummy', <class 'junk.Dummy'>), ('foo', {'two': 2, 'one': 1})]
>>> inspect.isclass(junk.foo)
True

I expected that inspect would only return Dummy since that is the only class definition in the module. Apparently, though, junk.foo is a class in the eyes of the inspect module. Why is that?

我希望inspect只返回Dummy,因为这是模块中唯一的类定义。显然,junk.foo是检查模块眼中的一个类。这是为什么?

2 个解决方案

#1


10  

Prior to Python v2.7, inspect.isclass naively assumed anything with a __bases__ attribute must be a class.

在Python v2.7之前,inspect.isclass天真地假设任何带有__bases__属性的东西必须是一个类。

Dummy's __getattr__ makes Dummy instances appear to have every attribute (with a value of None).

Dummy的__getattr__使Dummy实例看起来具有每个属性(值为None)。

Therefore, to inspect.isclass, foo appears to be a class.

因此,对于inspect.isclass,foo似乎是一个类。

Note: __getattr__ should raiseAttributeError when asked for an attribute it does not know about. (This is very different than returning None.)

注意:__getattr__在被要求提供它不知道的属性时应该是raiseAttributeError。 (这与返回None非常不同。)

#2


4  

First if all great answer Jon-Eric i just wanted to add some stuff:

首先,如果所有伟大的答案Jon-Eric我只是想添加一些东西:

if you do in ipython (what a great tool):

如果你在ipython中做(这是一个很棒的工具):

%psource inspect.isclass

you will get:

你会得到:

return isinstance(object, types.ClassType) or hasattr(object, '__bases__')

which what Jon-Eric said.

这是Jon-Eric所说的。

but i guess that you use python < 2.6 and this bug was already fixed, this is the code of inspect.isclass() in python2.7:

但我想你使用python <2.6并且这个bug已经修复了,这是python2.7中的inspect.isclass()代码:

return isinstance(object, (type, types.ClassType))

#1


10  

Prior to Python v2.7, inspect.isclass naively assumed anything with a __bases__ attribute must be a class.

在Python v2.7之前,inspect.isclass天真地假设任何带有__bases__属性的东西必须是一个类。

Dummy's __getattr__ makes Dummy instances appear to have every attribute (with a value of None).

Dummy的__getattr__使Dummy实例看起来具有每个属性(值为None)。

Therefore, to inspect.isclass, foo appears to be a class.

因此,对于inspect.isclass,foo似乎是一个类。

Note: __getattr__ should raiseAttributeError when asked for an attribute it does not know about. (This is very different than returning None.)

注意:__getattr__在被要求提供它不知道的属性时应该是raiseAttributeError。 (这与返回None非常不同。)

#2


4  

First if all great answer Jon-Eric i just wanted to add some stuff:

首先,如果所有伟大的答案Jon-Eric我只是想添加一些东西:

if you do in ipython (what a great tool):

如果你在ipython中做(这是一个很棒的工具):

%psource inspect.isclass

you will get:

你会得到:

return isinstance(object, types.ClassType) or hasattr(object, '__bases__')

which what Jon-Eric said.

这是Jon-Eric所说的。

but i guess that you use python < 2.6 and this bug was already fixed, this is the code of inspect.isclass() in python2.7:

但我想你使用python <2.6并且这个bug已经修复了,这是python2.7中的inspect.isclass()代码:

return isinstance(object, (type, types.ClassType))