【发布时间】:2010-11-02 20:30:10
【问题描述】:
给定以下模块:
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})
为什么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
我预计inspect 只会返回Dummy,因为这是模块中唯一的类定义。显然,junk.foo 在检查模块的眼中是一个类。这是为什么呢?
【问题讨论】:
标签: python introspection