Python: Positional Parameters
Function call, passing arguments by name=value pair
when a function is defined with positional parameters, e.g.
ff(x, y)
it can be called with positional syntax for arguments
ff(3, 4)
and it can also be called by name=value pairs for arguments, e.g.
ff(x=3, y=4)
# 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 name=value pairs, 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'