欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中參數(shù)打包和解包的實現(xiàn)

 更新時間:2023年09月04日 15:27:48   作者:python收藏家  
在Python中,打包和解包參數(shù)是一種操作方式,可以將多個參數(shù)打包成一個元組或字典,也可以將一個元組或字典解包成多個參數(shù),本文就來介紹一下如何使用

我們使用兩個運算符 *(用于元組)和 **(用于字典)。

考慮這樣一種情況:我們有一個函數(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)文章

最新評論