What Are Python Asterisk and Slash Special Parameters For?
Whenever you think of Python’s asterisk operator (*
), you most likely think of multiplication or exponentiation. Similarly, you probably associate the forward slash operator (/
) with division. But you can also use the bare asterisk and slash as special parameters in function headers. These do something completely unrelated to mathematics.
When defining functions, you’ll often include a list of comma-separated parameters to define what types of arguments the user can pass to your function. Using the asterisk and forward slash symbols as special parameters in your function header may look strange at first:
def strange_function(*, x, y):
...
def another_strange_function(a, b, /, c, *, d):
...
Both of these function definitions may look a bit odd, but their function parameters are actually perfectly valid. So, what exactly do a bare asterisk and slash mean in a Python function definition?
Get Your Code: Click here to download the free sample code for using the bare asterisk and slash as special parameters in your Python functions.
In Short: The Python Asterisk and Slash Control How to Pass Values to Functions
The asterisk (*
) and forward slash (/
) define whether you can pass positional or keyword arguments to your functions.
You use a bare asterisk to define a boundary between arguments that you can pass by either position or keyword from those that you must pass by keyword. You use the slash to define a boundary between those arguments that you must pass by position from those that you can pass by either position or keyword. Here’s a visual that summarizes how to use these symbols:
Left side | Divider | Right side |
---|---|---|
Positional-only arguments | / |
Positional or keyword arguments |
Positional or keyword arguments | * |
Keyword-only arguments |
You can also use both symbols together. You’ll learn more about this last point later, but for now take a look at the uses of both symbols individually.
Suppose you write an asterisk_usage()
function with an asterisk as one of its parameters. Then you call it:
>>> def asterisk_usage(either, *, keyword_only):
... print(either, keyword_only)
...
>>> asterisk_usage(either="Frank", keyword_only="Dean")
Frank Dean
>>> asterisk_usage("Frank", keyword_only="Dean")
Frank Dean
>>> asterisk_usage("Frank", "Dean")
Traceback (most recent call last):
File "", line 1, in
TypeError: asterisk_usage() takes 1 positional argument but 2 were given
You define your function’s either
parameter before the asterisk, meaning that you can pass arguments to it by keyword or position. Your function’s keyword_only
parameter can accept arguments by keyword only because you defined it after the asterisk. Your first two function calls succeed because you pass the second argument by keyword. Notice that passing the first argument by keyword or position works just fine.
Your third function call fails because you attempted to pass the second argument by position. The error message tells you that you’ve used one too many positional arguments.
Note: If you decide to use the asterisk, then you can’t use it as the final parameter in your function. The asterisk is designed to force you to pass all subsequent parameters by keyword. If you place it last, then there can be no subsequent parameters:
>>> def print_three_members(member1, member2, *):
File "", line 1
def print_three_members(member1, member2, *):
^
SyntaxError: named arguments must follow bare *
As you can see above, you won’t get past the function definition line.
Now suppose you write a slash_usage()
function with a slash as part of its header. Try it out:
>>> def slash_usage(positional_only, /, either):
... print(positional_only, either)
...
>>> slash_usage("Frank", either="Dean")
Frank Dean
>>> slash_usage(positional_only="Frank", either="Dean")
Traceback (most recent call last):
File "", line 1, in
TypeError: slash_usage() got some positional-only arguments
passed as keyword arguments: 'positional_only'
>>> slash_usage("Frank", "Dean")
Frank Dean
This time, you used the slash to ensure that you must pass any preceding arguments by position. When you pass in positional_only
by position, your call works. However, when you pass the same argument by keyword, your call fails. Your final function call shows that you can pass the either
argument by either position or by keyword.
Note: The Python documentation refers to *
and /
as both symbols and special parameters. In this tutorial, you’ll see both terms where appropriate. The term operators will only refer to their use in multiplication and division. Also, the term arguments will refer to the data that you’re passing to a function, while parameters will refer to the function header placeholders that are receiving it.
PEP 3102 – Keyword-Only Arguments defines the use of the asterisk in terms of an argument, while PEP 570 – Python Positional-Only Parameters defines the slash in terms of a parameter, although symbol also shows up briefly. PEP 570 also uses the words parameter and argument interchangeably.
Now you know how to use the asterisk and slash when defining your own function. But perhaps you have some questions about what kinds of functions these enable you to write, and maybe you’re also thinking about the asterisk in *args
. Keep reading for answers!
Can You Write a Function That Accepts Only Keyword Arguments?
Earlier, you learned that you can’t include the asterisk as the final parameter when defining your function. So you might be surprised to learn that it can be your first parameter. In that case, you’re specifying that your function will only accept keyword arguments.
In Python, a keyword argument is one that you pass to a function using its parameter name—for example, first_name="Dean"
. Passing your arguments by keyword makes your code more readable and also makes using the function more meaningful to your users. You can pass keyword arguments in any order, but they must come after positional arguments, if applicable.
You already know that parameters defined after the asterisk only take keyword arguments. To write a function that only accepts keyword arguments, you define the asterisk special parameter first:
Read the full article at https://realpython.com/python-asterisk-and-slash-special-parameters/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]