Python:只使用dir()获取导入模块中定义的类?

时间:2022-10-07 23:15:01

I've got a module written in Python. I now want to import it into another script and list all classes I defined in this module. So I try:

我有一个用Python编写的模块。我现在想将它导入另一个脚本并列出我在此模块中定义的所有类。所以我尝试:

>>> import my_module
>>> dir(my_module)
['BooleanField', 'CharField', 'DateTimeField', 'DecimalField', 'MyClass', 'MySecondClass', 'ForeignKeyField', 'HStoreField', 'IntegerField', 'JSONField', 'TextField', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'datetime', 'db', 'division', 'os', 'struct', 'uuid']

The only two classes which I defined in my_module are MyClass and MySecondClass, the other stuff are all things that I imported into my_module.

我在my_module中定义的唯一两个类是MyClass和MySecondClass,其他的东西都是我导入my_module的东西。

I now want to somehow be able to get a list of all classes which are defined in my_module without getting all the other stuff. Is there a way to do this in Python?

我现在想要以某种方式获得my_module中定义的所有类的列表,而不会获得所有其他内容。有没有办法在Python中执行此操作?

3 个解决方案

#1


16  

Use the inspect module to inspect live objects:

使用inspect模块检查活动对象:

>>> import inspect
>>> import my_module
>>> [m[0] for m in inspect.getmembers(my_module, inspect.isclass) if m[1].__module__ == 'my_module']

That should then work, getting every class defined within that my_module.

然后应该工作,在my_module中定义每个类。

#2


4  

You can use Python Class Browser

您可以使用Python类浏览器

import pyclbr
module_name = 'your_module'
module_info = pyclbr.readmodule(module_name)
print(module_info)

for item in module_info.values():
    print(item.name)

it will list all classes defined in your_module

它将列出your_module中定义的所有类

#3


0  

If you really want to use dir(), here it is:

如果你真的想使用dir(),这里是:

>>> import module
>>> [eval("module." + objname) for objname in dir(module) if type(eval("module." + objname)) is type]

or (script form)

或(脚本形式)

import module
classes = []
for objname in dir(module):
    obj = eval("module." + objname)
    if type(obj) is type:
        classes.append(obj)
print(classes)

But this uses the "eval" function, and is really not safe for use. I'd stick to the chosen answer

但是这使用了“eval”功能,实际上并不安全。我坚持选择的答案

#1


16  

Use the inspect module to inspect live objects:

使用inspect模块检查活动对象:

>>> import inspect
>>> import my_module
>>> [m[0] for m in inspect.getmembers(my_module, inspect.isclass) if m[1].__module__ == 'my_module']

That should then work, getting every class defined within that my_module.

然后应该工作,在my_module中定义每个类。

#2


4  

You can use Python Class Browser

您可以使用Python类浏览器

import pyclbr
module_name = 'your_module'
module_info = pyclbr.readmodule(module_name)
print(module_info)

for item in module_info.values():
    print(item.name)

it will list all classes defined in your_module

它将列出your_module中定义的所有类

#3


0  

If you really want to use dir(), here it is:

如果你真的想使用dir(),这里是:

>>> import module
>>> [eval("module." + objname) for objname in dir(module) if type(eval("module." + objname)) is type]

or (script form)

或(脚本形式)

import module
classes = []
for objname in dir(module):
    obj = eval("module." + objname)
    if type(obj) is type:
        classes.append(obj)
print(classes)

But this uses the "eval" function, and is really not safe for use. I'd stick to the chosen answer

但是这使用了“eval”功能,实际上并不安全。我坚持选择的答案