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:

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:

Through its holistic approach, REPOS empowers the open source community to thrive, while driving innovation.

REPOS presents opportunities for organizations to efficiently engage with and support the open source communities they rely on. Open source projects are the key behind most of today’s technological advancements, including AI, data, infrastructure, and cybersecurity. 

Participating in funding the open source projects you depend on is critical. Not only does it ensure that you can rely on the project long term, but you get a seat at the table and can talk directly to maintainers who influence or determine the direction of the project. This can reduce both your direct development and maintenance costs significantly. By sponsoring open source, organizations can also get access to key community participants who can help you grow your team (either directly or indirectly through their network).

Open source libraries and frameworks like PyTorch, JAX, and NumPy form the foundation for cutting-edge AI models and applications. Through REPOS, organizations can directly invest in these open source projects both by proposing open source work that they’re eager to fund and by funding the proposed work of the maintainers and contributors they rely on.

Open source contributors and maintainers can be challenging to contract with as you would need to maintain contracts with multiple organizations and individuals to connect with the right people. REPOS simplifies the process entirely by using OpenTeams as the single point of accountability for project success.  

Your organization maintains a standard Master Service Agreement with OpenTeams and each funded project becomes its own specialized Statement of Work extending from the innovative work of Quansight which created the “Community Work Order.” This direct engagement allows organizations to shape the future of technologies aligned with their specific needs and goals while building a bridge to the maintainers and contributors trusted by the projects.

Although the funding of these projects precedes our official launch and the Proposal Author is therefore an internal team member who described the funding happening, REPOS has already garnered attention from notable open source projects, and we look forward to more and more proposals and funded activities coming now that we’ve launched.

Share

Related Articles