在python中导入模块是通过关键字import
进行导入的,下面演示一下,模块的导入,指定模块别名,指定函数别名,调用模块中所有的函数
运行结果:
1、模块的导入
Study.py文件
里面的内容是:
形式参数*toppings
前面加上*会默认可以传入不限数量的实际参数
#usr/bin/env python3
#coding:utf-8
def build_profile(size, *toppings):
print("\n我要的尺寸是 " + str(size) + "cm" + " ")
for topping in toppings:
print("食物名称是: " + topping)
在另一个py文件中调用这个写好的模块
makeing_pizzas.py文件
1、方式一
直接用模块名导入,调用的时候是 模块名+函数名
import Study
Study.build_profile(16, '披萨')
Study.build_profile(12, '汉堡包')
2、方式二
指定导入的模块中的函数
内容如下:
from Study import build_profile
build_profile(16, '披萨')
build_profile(12, '汉堡包')
3、方式三
给模块(自己写的文件)指定别名,在调用的时候使用别名,在模块名名太长的时候使用比较方便
内容如下:
import Study as t
t.build_profile(16, '披萨')
t.build_profile(12, '汉堡包')
4、方式四
指定函数别名,在导入模块的前提下
内容如下:
from Study import build_profile as t
t(16, '披萨')
t(12, '汉堡包')
5、方式五
此种方式将指定模块下所有的函数都默认调用
内容如下:
from Study import *
build_profile(16, '披萨') #直接加Study.py中需要用到的函数名
build_profile(12, '汉堡包')