Python: Positional Parameters

By Xah Lee. Date: .

Function call, passing arguments by name=value pair

positional parameters of functions can be called with name=value pair.

# function with positional parameters
def ff(x, y):
    return [x, y]

# in function call, using positional args.
print(ff(3, 4))
# [3, 4]

# in function call, args can be passed by name=value pairs
print(ff(x=3, y=4))
# [3, 4]

Order does not matter

# function with positional parameters
def ff(x, y):
    return [x, y]

# when args are passed by param-name/value pair, order does not matter
print(ff(y=4, x=3))
# [3, 4]

Mixing positional arguments and keyword argument

in function call, positional arguments must come first, followed by keyword argument.

# function with positional parameters
def ff(x, y):
    return [x, y]

print(ff(4, y=3))
# [4, 3]

Example. error. keyword arg before positional arg

# function with positional parameters
def ff(x, y):
    return [x, y]

# error
print(ff(x=3,y))

    # print(ff(x=3,y))
                  # ^
# SyntaxError: positional argument follows keyword argument

Example. error. keyword argument already filled by positional arg

keyword argument must not be already filled by positional argument.

# function with positional parameters
def ff(x, y):
    return [x, y]

# error
print(ff(4, x=3))

    # print(ff(4, x=3))
          # ~~^^^^^^^^
# TypeError: ff() got multiple values for argument 'x'