Python|将字典作为关键字参数传递
很多时候,在使用Python字典时,由于 OOP 范式的出现,模块化集中在编程的不同方面。因此,在许多用例中,我们需要将字典作为参数传递给函数。但这需要将字典键解包为参数,并将其值作为参数值。让我们讨论一种可以执行此操作的方法。
方法:使用**
( splat )运算符
该运算符用于解包字典,在传入函数时,这可能会解包字典并实现将键映射到参数并将其值映射到参数值的所需任务。
# Python3 code to demonstrate working of
# Passing dictionary as keyword arguments
# Using ** ( splat ) operator
# Helper function to demo this task
def test_func(a = 4, b = 5):
print("The value of a is : " + str(a))
print("The value of b is : " + str(b))
# initializing dictionary
test_dict = {'a' : 1, 'b' : 2}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Testing with default values
print("The default function call yields : ")
test_func()
print("\r")
# Passing dictionary as keyword arguments
# Using ** ( splat ) operator
print("The function values with splat operator unpacking : ")
test_func(**test_dict)
输出 :
The original dictionary is : {'a': 1, 'b': 2}
The default function call yields :
The value of a is : 4
The value of b is : 5
The function values with splat operator unpacking :
The value of a is : 1
The value of b is : 2