I've already seen the following question but it doesn't quite get me where I want: How can I get a list of all classes within current module in Python?
我已经看到了以下问题,但它并没有让我想到我想要的地方:如何在Python中获取当前模块中所有类的列表?
In particular, I do not want classes that are imported, e.g. if I had the following module:
特别是,我不想要导入的类,例如如果我有以下模块:
from my.namespace import MyBaseClass
from somewhere.else import SomeOtherClass
class NewClass(MyBaseClass):
pass
class AnotherClass(MyBaseClass):
pass
class YetAnotherClass(MyBaseClass):
pass
If I use clsmembers = inspect.getmembers(sys.modules[__name__], inspect.isclass)
like the accepted answer in the linked question suggests, it would return MyBaseClass
and SomeOtherClass
in addition to the 3 defined in this module.
如果我使用clsmembers = inspect.getmembers(sys.modules [__ name __],inspect.isclass),就像链接问题中的接受答案所示,除了此模块中定义的3之外,它还将返回MyBaseClass和SomeOtherClass。
How can I get only NewClass
, AnotherClass
and YetAnotherClass
?
我怎样才能获得NewClass,AnotherClass和YetAnotherClass?
5 个解决方案
#1
24
Inspect the __module__
attribute of the class to find out which module it was defined in.
检查类的__module__属性以找出它所定义的模块。
#2
11
I apologize for answering such an old question, but I didn't feel comfortable using the inspect module for this solution. I read somewhere that is wasn't safe to use in production.
我为回答这么老的问题而道歉,但我觉得使用检查模块来解决这个问题感觉不舒服。我读过某些在生产中使用不安全的地方。
Initialize all the classes in a module into nameless objects in a list
将模块中的所有类初始化为列表中的无名对象
See Antonis Christofides comment to answer 1.
请参阅Antonis Christofides评论回答1。
I got the answer for testing if an object is a class from How to check whether a variable is a class or not?
我得到了测试一个对象是否来自如何检查变量是否为类的类的答案?
So this is my inspect-free solution
所以这是我的免检测解决方案
def classesinmodule(module):
md = module.__dict__
return [
md[c] for c in md if (
isinstance(md[c], type) and md[c].__module__ == module.__name__
)
]
classesinmodule(modulename)
#3
7
You may also want to consider using the "Python class browser" module in the standard library: http://docs.python.org/library/pyclbr.html
您可能还需要考虑使用标准库中的“Python类浏览器”模块:http://docs.python.org/library/pyclbr.html
Since it doesn't actually execute the module in question (it does naive source inspection instead) there are some specific techniques it doesn't quite understand correctly, but for all "normal" class definitions, it will describe them accurately.
由于它实际上并没有执行有问题的模块(它进行了简单的源代码检查),因此有一些特定的技术并不能正确理解,但对于所有“普通”类定义,它会准确地描述它们。
#4
7
I used the below:
我使用了以下内容:
# Predicate to make sure the classes only come from the module in question
def pred(c):
return inspect.isclass(c) and c.__module__ == pred.__module__
# fetch all members of module __name__ matching 'pred'
classes = inspect.getmembers(sys.modules[__name__], pred)
I didn't want to type the current module name in
我不想输入当前的模块名称
#5
2
from pyclbr import readmodule
clsmembers = readmodule(__name__).items()
#1
24
Inspect the __module__
attribute of the class to find out which module it was defined in.
检查类的__module__属性以找出它所定义的模块。
#2
11
I apologize for answering such an old question, but I didn't feel comfortable using the inspect module for this solution. I read somewhere that is wasn't safe to use in production.
我为回答这么老的问题而道歉,但我觉得使用检查模块来解决这个问题感觉不舒服。我读过某些在生产中使用不安全的地方。
Initialize all the classes in a module into nameless objects in a list
将模块中的所有类初始化为列表中的无名对象
See Antonis Christofides comment to answer 1.
请参阅Antonis Christofides评论回答1。
I got the answer for testing if an object is a class from How to check whether a variable is a class or not?
我得到了测试一个对象是否来自如何检查变量是否为类的类的答案?
So this is my inspect-free solution
所以这是我的免检测解决方案
def classesinmodule(module):
md = module.__dict__
return [
md[c] for c in md if (
isinstance(md[c], type) and md[c].__module__ == module.__name__
)
]
classesinmodule(modulename)
#3
7
You may also want to consider using the "Python class browser" module in the standard library: http://docs.python.org/library/pyclbr.html
您可能还需要考虑使用标准库中的“Python类浏览器”模块:http://docs.python.org/library/pyclbr.html
Since it doesn't actually execute the module in question (it does naive source inspection instead) there are some specific techniques it doesn't quite understand correctly, but for all "normal" class definitions, it will describe them accurately.
由于它实际上并没有执行有问题的模块(它进行了简单的源代码检查),因此有一些特定的技术并不能正确理解,但对于所有“普通”类定义,它会准确地描述它们。
#4
7
I used the below:
我使用了以下内容:
# Predicate to make sure the classes only come from the module in question
def pred(c):
return inspect.isclass(c) and c.__module__ == pred.__module__
# fetch all members of module __name__ matching 'pred'
classes = inspect.getmembers(sys.modules[__name__], pred)
I didn't want to type the current module name in
我不想输入当前的模块名称
#5
2
from pyclbr import readmodule
clsmembers = readmodule(__name__).items()