尝试检查模块。
getmembers并且各种测试应该会有所帮助。
编辑:
例如,
class MyClass(object): a = '12' b = '34' def myfunc(self): return self.a>>> import inspect>>> inspect.getmembers(MyClass, lambda a:not(inspect.isroutine(a)))[('__class__', type), ('__dict__', <dictproxy {'__dict__': <attribute '__dict__' of 'MyClass' objects>, '__doc__': None, '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'MyClass' objects>, 'a': '34', 'b': '12', 'myfunc': <function __main__.myfunc>}>), ('__doc__', None), ('__module__', '__main__'), ('__weakref__', <attribute '__weakref__' of 'MyClass' objects>), ('a', '34'), ('b', '12')]现在,特殊的方法和属性引起了我的共鸣-可以通过多种方式来处理这些方法和属性,其中最简单的方法就是根据名称进行过滤。
>>> attributes = inspect.getmembers(MyClass, lambda a:not(inspect.isroutine(a)))>>> [a for a in attributes if not(a[0].startswith('__') and a[0].endswith('__'))][('a', '34'), ('b', '12')]…,其中更复杂的可以包括特殊的属性名称检查甚至元类;)



