Python: Function Rest Parameters (Single Asterisk *)

By Xah Lee. Date: .

Unspecified Number of Positional Parameters (Aka Rest Parameters) (Single Asterisk *)

To define unspecified number of positional parameters, use *name as last item. The function receive it as a Tuple.

💡 TIP: this is useful if you need to define a function such as sum.

# function with unspecified number of args
def ff(*x):
    # x is received as a tuple.
    return x

rr = ff(5,6)

print(rr)
# (5, 6)

print(type(rr))
# <class 'tuple'>
# example of position param with rest param
def ff(a, *x):
    return [a, x]

rr = ff(5, 6, 7)

print(rr == [5, (6, 7)])
# [5, (6, 7)]

Python, Function and Class