How To Define Function Parameters As Positional, Keyword, or Both in Python
Start using the new / and * symbols in your function definitions
As of Python 3.8, it is now possible to define function parameters as positional only, keyword only, or keyword optional.
Let’s talk through what functions are, the types of function parameters, and finally, how to define parameters as positional, keyword, or either.
What Are Functions?
Very briefly, a function is a defined sequence of statements that can be repeatedly invoked—referred to as a function call.
Think of a function’s definition as being a black-box, meaning that what happens in the function stays in the function. The exception to this rule is specified inputs (called parameters) and a specified output (called a return).
Use functions to create a sequence of statements that accomplish smaller tasks in a controlled environment and benefit with code that performs better and is easier to maintain.
Types of Function Parameters
When defining custom functions, the variable(s) that hold values being passed in are called parameters.
Traditionally, the order of parameter lists are critical in that the order of the parameters and arguments match up one-to-one. These are called positional parameters.def myFunction(a,b,c):
print(a,b,c)myFunction(1,2,3) # 1,2,3
In Python, functions parameters can also be specified as keyword parameters. In this scenario, the parameter’s name is included when calling the function, thus explicitly declaring which value goes where.def myFunction(a,b,c):
print(a,b,c)myFunction(b=2,c=3,a=1) 1,2,3
Also, it is possible to define a default value for a parameter, thus making it optional.def myFunction(a,b,c=None):
print(a,b,c)myFunction(1,2,3) # 1,2,3
myFunction(1,2) # 1,2,None
Using keyword arguments is helpful when some parameters in a function are optional and you only want to specify a subset of the parameters.def myFunction(a,b,c=None,d=None):
print(a,b,c,d)myFunction(1,2,d=4) # 1,2,None,4
# no need to pass a value for c
Be mindful that while arguments are defaulted to being positional, once an argument is passed as a keyword, all arguments thereafter in the call are required to be keyword as well.
You cannot jump back and forth between positional and keyword.def myFunction(a,b,c):
print(a,b,c)myFunction(1,b=2,3) # will cause an error
# SyntaxError: positional argument follows keyword argument
This flexibility does come with consequences and concerns. Given our previous code snippet, if we change the name of the parameter inside the function, calls that refer to that parameter as a keyword would break.def myFunction(x,y,z):
print(a,b,c)myFunction(1,2,3) # still works with new definition
myFunction(c=3,b=2,a=1) # this is no longer valid
How To Define Parameters As Positional, Keyword, or Both
Starting in Python 3.8, it is now possible to explicitly define parameters as being positional only, keyword optional, or keyword only.
Using the / and * symbols, parameters will have the aforementioned properties based on where they are relative to the symbols.positional only / keyword optional * keyword only
We can see this in action by modifying our original function. We’ll make a positional only, b keyword optional, and c keyword only.def myFunction(a,/,b,*,c):
print(a,b,c)myFunction(1,2,3)
# TypeError: myFunction() takes 2 positional arguments but 3 were givenmyFunction(a=1,b=2,c=3)
# TypeError: myFunction() got some positional-only arguments passed as keyword arguments: 'a'# valid function calls
myFunction(1,2,c=3)
myFunction(1,b=2,c=3)
Conclusion
How will you use positional only, keyword optional, and keyword-only parameters?
According to PEP 570, using positional only arguments allows for arguments with the same name as the parameter to be used via **kwargs. What do you think?