簡體   English   中英

如何存儲要由多個函數使用的相同參數

[英]How do I store identical arguments to be used by multiple functions

我有多個將使用相同參數的函數。 是否可以僅將參數存儲到變量中,以便可以將變量傳遞給多個函數?

例:

#Store the arguments into a variable:
Arguments = (pos_hint={'center_x': 0.5, 'center_y': 0.5},
             size_hint=(1, 1), duration=2) #possible?


function1(Arguments) #Then pass variable to first function
function2(Arguments) #Then pass variable to different function
function3(Arguments) #etc.
...

您可以將參數存儲在字典中,然后使用**解包語法

Arguments = {
    'pos_hint': {'center_x': 0.5, 'center_y': 0.5},
    'size_hint': (1, 1),
    'duration': 2
}

function1(**Arguments)
function2(**Arguments)
function3(**Arguments)

下面是一個演示:

>>> def func(a, b, c):
...     return a + b + c
...
>>> dct = {'a':1, 'b':2, 'c':3}
>>> func(**dct)
6
>>>

基本上,請執行以下操作:

func(**dct)

等效於:

func(a=1, b=2, c=3)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM