如何在一个.py文件中获取所有类的列表?

时间:2013-03-27 15:40:36

标签: python

mm.py

class A():
    def __init__(self):
           pass
class B():
    def __int__(slef):
           pass

如何生成mm.py中所有类的列表?

3 个答案:

答案 0 :(得分:6)

您可以使用inspect

import myModule

import inspect    
print inspect.getmembers(myModule, inspect.isclass)

答案 1 :(得分:0)

您可以使用dir(模块)导入模块并扫描其中的所有名称:

import mymodule
import types


for item in dir(mymodule):
    itemtype = type(item)
    if itemtype == types.ClassType:
        print item + "is old style class"
    if itemtype == types.TypeType:
        print item + "is new style class"

针对新旧样式类进行了更新

答案 2 :(得分:0)

可以使用locale()函数进行过滤。