学习内容:《python编程:从入门到实践》第二版
知识点:
模块,函数存储在模块中,导入模块中的函数
函数编写指南
练习内容:
练习8-15:打印模型
将示例printing_models.py中的函数放在一个名为printing_functions.py的文件中。在printing_models.py的开头编写一条import语句,并修改该文件以使用导入的函数。
练习8-16:导入
选择一个你编写的且只包含一个函数的程序,将该函数放在另一个文件中。在主程序文件中,使用下述各种方法导入这个函数,再调用它:[插图]
我的代码&运行结果:
练习8-15:打印模型
将示例printing_models.py中的函数放在一个名为printing_functions.py的文件中。在printing_models.py的开头编写一条import语句,并修改该文件以使用导入的函数。
import printing_functions
unprinted_designs = ['phone case','robot pendant','dodecahedron']
completed_models = []
printing_functions.print_models(unprinted_designs,completed_models)
printing_functions.show_completed_models(completed_models)
注意点:
1.在printing_functions.py中,删除主程序
作为数据库的两个列表
练习8-16:导入
选择一个你编写的且只包含一个函数的程序,将该函数放在另一个文件中。在主程序文件中,使用下述各种方法导入这个函数,再调用它:
import module_name
from module_name import function_name
from module_name import function_name as fn
import module_name as mn
from module_name import *
print("\n方法二")
from printing_functions import print_models,show_completed_models
unprinted_designs = ['phone case','robot pendant','dodecahedron']
completed_models = []
printing_functions.print_models(unprinted_designs,completed_models)
printing_functions.show_completed_models(completed_models)
print("\n方法三")
#当模块中包含两个函数时,无法使用
print("\n方法四")
import printing_functions as pf
unprinted_designs = ['phone case','robot pendant','dodecahedron']
completed_models = []
pf.print_models(unprinted_designs,completed_models)
pf.show_completed_models(completed_models)
print("\n方法五")
from printing_functions import *
unprinted_designs = ['phone case','robot pendant','dodecahedron']
completed_models = []
printing_functions.print_models(unprinted_designs,completed_models)
printing_functions.show_completed_models(completed_models)