在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, '漢堡包')