在导入程序中定义需要被包调用的函数,并将其包装在一个模块中。然后,包可以导入并使用这些函数。例如:
在导入程序中定义函数:
def my_function():
print("Hello from my_function!")
将其包装在一个名为“my_module”的模块中:
my_module/
__init__.py
my_functions.py
在my_functions.py文件中定义函数my_function:
def my_function():
print("Hello from my_function!")
在__init__.py文件中导入my_function:
from my_module.my_functions import my_function
然后,在包中可以调用my_function:
import my_module
my_module.my_function()
这样,包中的代码就可以调用导入程序中定义的函数了。