這個問題在這里已經有了答案: 函式引數中的空星號? (6 個回答) 4 小時前關閉。
嗨,我有幾行 python 代碼,這讓我很困惑:
def apply_async(func, args,*, callback):
# Compute the result
result = func(*args)
# Invoke the callback with the result
callback(result)
def print_result(result):
print('Got:', result)
def add(x, y):
return x y
apply_async(add, (2, 3), callback=print_result)
我知道要撰寫一個接受任意數量位置引數的函式,請使用 * 引數。
但我不明白為什么*會出現在args。然后*args可以代表(2,3)這個例子中的元組?
uj5u.com熱心網友回復:
這是https://www.python.org/dev/peps/pep-3102。之后的所有引數都*必須是命名引數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/334171.html
標籤:Python
