是否可以列出模块中的所有功能?
我以这种格式定义了一个 .py 文件:
foo.py
def foo1(): pass
def foo2(): pass
def foo3(): pass
我从另一个文件导入它:
main.py
from foo import *
# or
import foo
是否可以列出所有函数名称,例如 ["foo1", "foo2", "foo3"]?
感谢您的帮助,我做了一个我想要的课程,如果您有建议,请发表评论
class GetFuncViaStr(object):
def __init__(self):
d = {}
import foo
for y in [getattr(foo, x) for x in dir(foo)]:
if callable(y):
d[y.__name__] = y
def __getattr__(self, val) :
if not val in self.d :
raise NotImplementedError
else:
return d[val]
I defined a .py file in this format:
foo.py
def foo1(): pass
def foo2(): pass
def foo3(): pass
I import it from another file:
main.py
from foo import *
# or
import foo
Is it possible list all functions name, e.g. ["foo1", "foo2", "foo3"]
?
Thanks for your help, I made a class for what I want, pls comment if you have suggestion
class GetFuncViaStr(object):
def __init__(self):
d = {}
import foo
for y in [getattr(foo, x) for x in dir(foo)]:
if callable(y):
d[y.__name__] = y
def __getattr__(self, val) :
if not val in self.d :
raise NotImplementedError
else:
return d[val]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对于狂野导入,
您可以使用不带参数的 dir() 来显示当前模块名称空间中的对象。这很可能不仅仅包括
foo
的内容。如果是绝对导入(顺便说一下,您应该更喜欢这种方式),您可以将模块传递给
dir()
:另请检查
dir
的文档。由于您只需要函数,因此您可能需要考虑使用inspect.isfunction
。希望您不要将该列表用于非调试目的。For a wild import
you can use
dir()
without a parameter to show objects in the current module's namespace. This will most probably include more than just the content offoo
.In case of an absolute import (which you should prefer by the way) you can pass the module to
dir()
:Also check the documentation of
dir
. As you only wanted functions, you might want to think about usinginspect.isfunction
. Hope you don't use that list for non-debugging purposes.如果想列出当前模块的函数(即不是导入的函数),你也可以这样做:
if wanting to list functions of the current module (i.e., not an imported one), you could also do something like this:
执行这些操作的最简洁方法是使用检查模块。它有一个 getmembers 函数,该函数将谓词作为第二个参数。您可以使用
isfunction
作为谓词。现在,all_functions 将是一个元组列表,其中第一个元素是函数的名称,第二个元素是函数本身。
The cleanest way to do these things is to use the inspect module. It has a
getmembers
function that takes a predicate as the second argument. You can useisfunction
as the predicate.Now,
all_functions
will be a list of tuples where the first element is the name of the function and the second element is the function itself.您可以使用 dir 来探索名称空间。
示例:在 shell 中加载 foo
您可以使用 getattr 来获取 foo 中的关联属性并确定它是否可调用。
检查文档: http://docs.python.org/tutorial/modules.html#the- dir-function
如果你这样做 - “from foo import *” 那么名称将包含在你调用它的名称空间中。
以下有关 python 内省的简介可能会对您有所帮助:
you can use dir to explore a namespace.
Example: loading your foo in shell
You can use getattr to get the associated attribute in foo and find out if it callable.
Check the documentation : http://docs.python.org/tutorial/modules.html#the-dir-function
and if you do - "from foo import *" then the names are included in the namespace where you call this.
The following brief on introspection in python might help you :
喜欢
aaronasterling 说,你可以使用getmembers 来自
inspect
模块的函数来执行此操作。但是,此将包含已在其他地方定义的函数,但导入到该模块的命名空间中。
如果您只想获取该模块中定义的函数,请使用以下代码片段:
Like
aaronasterling said, you can use the getmembers functions from the
inspect
module to do this.However, this will include functions that have been defined elsewhere, but imported into that module's namespace.
If you want to get only the functions that have been defined in that module, use this snippet:
尝试使用检查模块,如下所示,例如 if module -->温度.py
Try using inspect module like below for exmaple if module --> temp.py