获取模块的所有功能 [英] Get all functions of a module
问题描述
一个项目具有以下结构:
A project has the following structure:
modulename
├── __init__.py
│
├── one
│ ├── function_1.py
│ ├── function2.py
│ └─── __init__.py
│
└── two
├── another_function.py
├── yet_another_function.py
└─── __init__.py
每个.py
(除了__init__.py
都是空的)都具有以下内容:
Each .py
(except the __init__.py
's which are empty) has content along the lines of:
def foo(x):
return x
def bar(x):
return x + 2
要使用该模块,请通过以下方式将其导入:import modulename.one.function1.foo
.我想做的是在倒数第二个位置找到所有.py
文件名,例如function1
或another_function
.
To use the module you import it it in the following way: import modulename.one.function1.foo
. What I want to do is find all .py
file names in the second to last place, for example function1
or another_function
.
到目前为止,建议的解决方案未成功:
The suggested solutions so far has unsuccessfully been:
-
dir(modulename.one)
会导致['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
.
help(modulename.one)
实际上包括功能文件名称,标题为PACKAGE CONTENTS
.如何获取PACKAGE CONTENTS
的列表?
help(modulename.one)
which actually includes the function files' names under the title PACKAGE CONTENTS
. How do I get a list of the PACKAGE CONTENTS
?
我可以(如有人建议的那样)在__init__.py
中使用__all__
,但是我更喜欢一个简单的内置函数或模块.
I could (as someone suggested) use __all__
in the __init__.py
's, but I would prefer a simple built-in function or module.
推荐答案
我认为您正在寻找的是包__init__
文件中的__all__
定义.
I think what you are looking for is the __all__
definition in your packages __init__
files.
您可以看一下: http://mikegrouchy. com/blog/2012/05/be-pythonic -__ init__py.html
解释了软件包中__all__
的行为.
which explains the __all__
behavior in packages.
这篇关于获取模块的所有功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!