Python中參數(shù)打包和解包的實現(xiàn)
我們使用兩個運算符 *(用于元組)和 **(用于字典)。
考慮這樣一種情況:我們有一個函數(shù)接收四個參數(shù)。我們想調(diào)用這個函數(shù),我們有一個大小為4的列表,其中包含該函數(shù)的所有參數(shù)。如果我們簡單地傳遞一個列表給函數(shù),調(diào)用就不起作用。
# A sample function that takes 4 arguments
# and prints them.
def fun(a, b, c, d):
print(a, b, c, d)
# Driver Code
my_list = [1, 2, 3, 4]
# This doesn't work
fun(my_list)輸出
TypeError: fun() takes exactly 4 arguments (1 given)
解包
我們可以使用 * 來解包列表,這樣它的所有元素都可以作為不同的參數(shù)傳遞。
# A sample function that takes 4 arguments
# and prints the,
def fun(a, b, c, d):
print(a, b, c, d)
# Driver Code
my_list = [1, 2, 3, 4]
# Unpacking list into four arguments
fun(*my_list)輸出
(1, 2, 3, 4)
需要記住參數(shù)的長度必須與我們?yōu)閰?shù)解包的列表的長度相同。
# Error when len(args) != no of actual arguments
# required by the function
args = [0, 1, 4, 9]
def func(a, b, c):
return a + b + c
# calling function with unpacking args
func(*args)輸出
Traceback (most recent call last):
File "/home/592a8d2a568a0c12061950aa99d6dec3.py", line 10, in <module>
func(*args)
TypeError: func() takes 3 positional arguments but 4 were given
再舉一個例子,考慮內(nèi)置的range()函數(shù),它需要單獨的開始和停止參數(shù)。如果它們不能單獨使用,請使用 *運算符編寫函數(shù)調(diào)用,以將參數(shù)從列表或元組中解包:
>>> >>> range(3, 6) # normal call with separate arguments [3, 4, 5] >>> args = [3, 6] >>> range(*args) # call with arguments unpacked from a list [3, 4, 5]
打包
當我們不知道有多少參數(shù)需要傳遞給Python函數(shù)時,我們可以將所有參數(shù)打包到元組中。
# A Python program to demonstrate use
# of packing
# This function uses packing to sum
# unknown number of arguments
def mySum(*args):
return sum(args)
# Driver code
print(mySum(1, 2, 3, 4, 5))
print(mySum(10, 20))輸出
15
30
上面的函數(shù)mySum()進行“打包”,將該方法調(diào)用接收到的所有參數(shù)打包到一個變量中。一旦我們有了這個’packed’變量,我們就可以用它來做我們用普通元組做的事情。args[0]和args[1]將分別給予第一個和第二個參數(shù)。由于我們的元組是不可變的,所以可以將args元組轉(zhuǎn)換為列表,這樣就可以修改、刪除和重新排列i中的項。
下面是一個展示打包和解包的示例。
# A Python program to demonstrate both packing and
# unpacking.
# A sample python function that takes three arguments
# and prints them
def fun1(a, b, c):
print(a, b, c)
# Another sample function.
# This is an example of PACKING. All arguments passed
# to fun2 are packed into tuple *args.
def fun2(*args):
# Convert args tuple to a list so we can modify it
args = list(args)
# Modifying args
args[0] = 'python'
args[1] = 'awesome'
# UNPACKING args and calling fun1()
fun1(*args)
# Driver code
fun2('Hello', 'beautiful', 'world!')輸出
(python, awesome, world!)
用于字典
# A sample program to demonstrate unpacking of
# dictionary items using **
def fun(a, b, c):
print(a, b, c)
# A call with unpacking of dictionary
d = {'a':2, 'b':4, 'c':10}
fun(**d)輸出
2 4 10
這里 ** 解包了與它一起使用的字典,并將字典中的項作為關(guān)鍵字參數(shù)傳遞給函數(shù)。所以寫“fun(1,**d)”相當于寫“fun(1,b=4,c=10)”。
# A Python program to demonstrate packing of
# dictionary items using **
def fun(**kwargs):
# kwargs is a dict
print(type(kwargs))
# Printing dictionary items
for key in kwargs:
print("%s = %s" % (key, kwargs[key]))
# Driver code
fun(name="geeks", ID="101", language="Python")輸出
<class 'dict'>
name = geeks
ID = 101
language = Python
應(yīng)用和要點
- 在套接字編程中用于向服務(wù)器發(fā)送大量請求。
- 在Django框架中用于將變量參數(shù)發(fā)送到視圖函數(shù)。
- 有一些包裝函數(shù)要求我們傳入變量參數(shù)。
- 參數(shù)的修改變得很容易,所以必須小心使用它們。
到此這篇關(guān)于Python中參數(shù)打包和解包的實現(xiàn)的文章就介紹到這了,更多相關(guān)Python參數(shù)打包和解包內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python 余弦相似度與皮爾遜相關(guān)系數(shù) 計算實例
今天小編就為大家分享一篇Python 余弦相似度與皮爾遜相關(guān)系數(shù) 計算實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python實現(xiàn)從文件中加載數(shù)據(jù)的方法詳解
日常工作中有許多類型的文件,以及許多方法,用它們從文件中提取數(shù)據(jù)來圖形化。本文將利用Python實現(xiàn)從文件中加載數(shù)據(jù),感興趣的可以了解一下2022-04-04
使用python+Flask實現(xiàn)日志在web網(wǎng)頁實時更新顯示
日志是一種可以追蹤某些軟件運行時所發(fā)生事件的方法,下面這篇文章主要給大家介紹了關(guān)于使用python+Flask實現(xiàn)日志在web網(wǎng)頁實時更新顯示的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-08-08
python實現(xiàn)數(shù)據(jù)挖掘中分箱的示例代碼
數(shù)據(jù)分箱(英語:Data?binning)是一種數(shù)據(jù)預(yù)處理方法,用于最大限度地減少小觀測誤差的影響,本文主要為大家介紹了python實現(xiàn)數(shù)據(jù)分箱的相關(guān)知識,感興趣的可以了解下2024-01-01
Python開發(fā)游戲自動化后臺腳本的實現(xiàn)
本文主要介紹了Python開發(fā)游戲自動化后臺腳本的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
python列表推導(dǎo)式實現(xiàn)找出列表中長度大于5的名字
這篇文章主要介紹了python列表推導(dǎo)式實現(xiàn)找出列表中長度大于5的名字,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02

