python小白有話說之(導入指定的模塊)

在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, '漢堡包')
發佈了123 篇原創文章 · 獲贊 201 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章