Ultima attività 10 months ago

這段 Python 程式碼 展示了 參數解包 (Argument Unpacking) 的應用方式,包括 列表解包 (*args) 和 字典解包 (**kwargs),用於函式呼叫時動態傳遞參數。

timmy ha revisionato questo gist 10 months ago. Vai alla revisione

Nessuna modifica

timmy ha revisionato questo gist 10 months ago. Vai alla revisione

1 file changed, 12 insertions

unpacking-argument-lists.py(file creato)

@@ -0,0 +1,12 @@
1 + assert list(range(3, 6)) == [3, 4, 5]
2 +
3 + arguments_list = [3, 6]
4 + assert list(range(*arguments_list)) == [3, 4, 5]
5 +
6 +
7 + def function_that_receives_names_arguments(first_word, second_word):
8 + return first_word + ", " + second_word + "!"
9 +
10 +
11 + arguments_dictionary = {"first_word": "Hello", "second_word": "World"}
12 + assert function_that_receives_names_arguments(**arguments_dictionary) == "Hello, World!"
Più nuovi Più vecchi