跳转到主要内容
eLearner.app
模块 5 · 第 3 课(共 4)课程中的19/36~12 min
模块课程(3/4)

*args 和 **kwargs

Sometimes you want a function that accepts a variable number of arguments. Python gives you two tools: *args (gathers positional arguments into a tuple) and **kwargs (gathers keyword arguments into a dict).

*args: variable positional arguments

Python
def somma_tutto(*nums):
    totale = 0
    for n in nums:
        totale += n
    return totale

somma_tutto(1, 2, 3)         # 6
somma_tutto(1, 2, 3, 4, 5)   # 15
somma_tutto()                # 0

Inside the function, nums is a tuple with all the values passed. The name is just a convention: *args is the most common, but *nums or *valori are perfectly valid.

**kwargs: variable keyword arguments

Python
def crea_config(**opzioni):
    return opzioni

crea_config(debug=True, port=8080)
# {'debug': True, 'port': 8080}

Inside the function, opzioni is a dict with {param_name: value}.

Combining everything

The fixed order is: **regular parameters, *args, default parameters, **kwargs**.

Python
def f(primo, *args, opzione="x", **kwargs):
    print(primo, args, opzione, kwargs)

f(1, 2, 3, opzione="y", extra=10)
# 1 (2, 3) y {'extra': 10}

Unpacking in the call: * and **

The same operator works the other way around: to "explode" a list/tuple into positional arguments or a dict into keyword arguments.

Python
def punto(x, y, z):
    return (x, y, z)

coords = [1, 2, 3]
punto(*coords)        # punto(1, 2, 3)  →  (1, 2, 3)

opts = {"x": 1, "y": 2, "z": 3}
punto(**opts)         # punto(x=1, y=2, z=3)

Wrapper pattern

Python
def log_chiamata(fn, *args, **kwargs):
    print(f"chiamo {fn.__name__} con {args=} {kwargs=}")
    return fn(*args, **kwargs)

log_chiamata(max, 3, 1, 2)             # calls max(3, 1, 2)
log_chiamata(sorted, [3, 1, 2], reverse=True)

Parameter ordering in function signatures

When defining a function that accepts multiple argument types, you must adhere to a strict order in the signature:

  1. Standard positional parameters.
  2. Positional varargs *args.
  3. Keyword parameters with default values.
  4. Keyword varargs **kwargs.

Try it

锻炼#python.m5.l3.e1
尝试:0加载中...

Define `average(*nums)` that returns the arithmetic mean (sum / count). Call it with 10, 20, 30 and assign the result to `m`. Evaluate `m`.

正在加载编辑器...
显示提示

sum(nums) / len(nums)

3 次尝试后可用的解决方案

Review exercise

锻炼#python.m5.l3.e2
尝试:0加载中...

Given the function `def point(x, y, z): return x + y + z` and the dict `coords = {'x': 1, 'y': 2, 'z': 3}`, call point unpacking coords and assign the result to `s`. Evaluate `s`.

正在加载编辑器...
显示提示

point(**coords)

3 次尝试后可用的解决方案

Additional challenge

锻炼#python.m5.l3.e3
尝试:0加载中...

Write a function `multiply_all(*args)` that returns the product of all numbers passed as arguments. If no arguments are passed, it should return `1`. Finally, evaluate the call `multiply_all(2, 3, 4)`.

正在加载编辑器...
显示提示

Use a for loop to iterate over the elements in args and accumulate their product.

3 次尝试后可用的解决方案