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?

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.

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.

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 ]

Related Articles

Kedro Open Source Development

In this episode of Open Source Directions, we were joined by Yetunde Dada talked about the work being done with Kedro. Kedro is an open-source Python framework that applies software engineering best practices to data and machine-learning pipelines. You can use it, for example, to optimize the process of taking a machine learning model into a production environment. You can use Kedro to organize a single-user project running on a local environment, or collaborate in a team on an enterprise-level project.

Open Source Databases

We had a very fun and engaging chat with Matt Yonkovit who is the Chief Experience Officer at Percona, a service provider for open source databases like MySQL, PostgreSQL, MariaDB, and RocksDB. Matt has worked as a database architect for 10 years before transitioning into consulting roles at both MySQL and Sun Microsystems. In total, he’s been working with databases and open source for nearly 25 years.

SymPy Open Source Development

In this episode of Open Source Directions, we were joined by Aaron Meurer who will talk once again with Oscar Benjamin about the work he has been doing with SymPy. SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) while keeping the code as simple as possible in order to be comprehensible and easily extensible. SymPy is written entirely in Python.

Technology Roundtable

Technology Roundtable is an opportunity for technology architects in the technology industry to learn, innovate and collaborate with their peers. Roundtable members work together on industry priorities and general topics of interest and concern related to open source technology initiatives.

Responses

Your email address will not be published. Required fields are marked *