好的,我知道您可以使用 dir() 方法列出模块中的所有内容,但是有什么方法可以仅查看该模块中定义的函数吗?例如,假设我的模块如下所示:
from datetime import date, datetime
def test():
return "This is a real method"
即使我使用检查()来过滤掉内置函数,我仍然保留了导入的任何内容。例如我会看到:
['日期'、'日期时间'、'测试']
有什么办法可以排除进口吗?或者另一种方式来找出模块中定义的内容?
您在寻找这样的东西吗?
import sys, inspect
def is_mod_function(mod, func):
return inspect.isfunction(func) and inspect.getmodule(func) == mod
def list_functions(mod):
return [func.__name__ for func in mod.__dict__.itervalues()
if is_mod_function(mod, func)]
print 'functions in current module:\n', list_functions(sys.modules[__name__])
print 'functions in inspect module:\n', list_functions(inspect)
编辑:将变量名称从“meth”更改为“func”以避免混淆(这里我们处理的是函数,而不是方法)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)