Python Glossary Русский

all Python 3Functionsterm

*args

parameters

Accepts any number of positional arguments as a tuple.

Syntax

def f(*args): ...

Inside the function args is a tuple, not a list, so you can't mutate it in place. Any parameter declared after *args becomes keyword-only — it can no longer be passed positionally, only by name. A star at the call site, f(*seq), does the reverse: it unpacks a sequence into separate arguments.

Examples

def my_sum(*args):
    return sum(args)
print(my_sum(1, 2, 3))  # → 6
print(my_sum(10, 20, 30, 40))  # → 100
def show(*args):
    for a in args:
        print(a)
show('a', 'b', 'c')  # → a / b / c
def first_last(*args):
    return args[0], args[-1]
print(first_last(1,2,3,4))  # → (1, 4)
def concat(*args, sep=' '):
    return sep.join(str(a) for a in args)
print(concat('x','y','z'))  # → x y z
nums = [4, 5, 6]
print(my_sum(*nums))  # → 15 (распаковка)
def mixed(a, b, *args):
    return a, b, args
print(mixed(1,2,3,4))  # → (1, 2, (3, 4))

See also

Open in the full glossarydocs.python.orgReport an inaccuracy