cat*_*t40 2 python module packages
我正在为我常用的函数构建一个自定义包,它有几个函数不适合任何特定模块并被多个模块使用。我已经把他们的__init__.py,和它的作品,但我已经看到了推荐一个非常小的很多教程__init__.py。有没有更好的地方可以放它们?
我希望能够像这样称呼他们:
import mypackage
#functions in modules
mypackage.module.function()
#common functions
mypackage.function()
Run Code Online (Sandbox Code Playgroud)
我把这些功能放在哪里?
您可以创建一个“实用程序”包/文件来存储可重复使用的代码段,然后将它们导入您的文件中,这可以从中受益!
至于你的 init.py 文件 - 我会把它留空!
# utilities.py
def method():
return None
# Some class
from utilities import *
nothing_burger = method()
print nothing_burger
Run Code Online (Sandbox Code Playgroud)
如果将父文件夹添加到 python 路径,则可以在 command_line_interface.py 或 data_access.py 文件中导入
from extras.utilities import *
您将可以访问所有可重复使用的方法
. Parent
|
??? cli
? ??? __init__.py
? ??? command_line_interface.py
??? persistence
? ??? data_access.py
? ??? __init__.py
??? extras
??? exceptions.py
??? utilities.py
??? __init__.py
Run Code Online (Sandbox Code Playgroud)