முக்கிய உள்ளடக்கத்திற்குச் செல்லவும்
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 முயற்சிகளுக்குப் பிறகு தீர்வு கிடைக்கும்