You are currently viewing Why Slash and Asterisk Used in Function Definition

Why Slash and Asterisk Used in Function Definition

When you read the documentation of some functions you see a slash (/) and an asterisk (*) passed in the function definition. Why are they passed in a function?

Why Slash and Asterisk are Used?

The slash (/) and asterisk (*) are used to determine how an argument should pass when a function is called.

Simply said, the parameters on the left side of the slash (/) must be supplied as positional-only arguments, whereas the parameters on the right side can be passed as either positional or keyword arguments.

An asterisk (*) indicates that the parameters on the right side must be supplied as keyword-only arguments, while the parameters on the left side can be passed as positional or keyword arguments.

Left-side ParametersSymbolRight-side Parameters
Positional-only arguments/Either positional or keyword arguments
Either positional or keyword arguments*Keyword-only arguments

Slash and Asterisk in Function Parameter

Here’s a simple example to show that when slashes and asterisks appear in a function definition, then parameters are bound to positional-only and keyword-only arguments, respectively.

You can conclude that x and y are positional-only parameters, while z is keyword-only. Let’s experiment to see if the parameters are bound to positional-only and keyword-only arguments.

The parameter x is supplied as a positional argument, while y and z are passed as keywords or named arguments. When you run the code, the following output will be produced.

Python raises a TypeError indicating that the positional-only parameter (y) was given as a keyword argument. If you simply supply y as a positional argument, the code will not generate any errors.

Can Asterisk Be Used Ahead of Slash

The asterisk (*) must come after the slash (/). Now, you may be wondering if this is a rule.

You may think of it as a rule, if you use both a slash and an asterisk, the slash must come before the asterisk, otherwise, Python will throw a syntax error.

The code has been modified, and the asterisk is used before the slash. When you run the code, you’ll get a syntax error.

Logically inserting an asterisk before the slash doesn’t make sense because the parameters on the right side of the asterisk are keyword-only, but the parameters on the left side of the slash are positional-only.

This will not work at all. Here is an example to demonstrate this situation.

The parameters a and b are between the asterisk and the slash, making it unclear if they are positional or keyword-only parameters. This code will still throw a syntax error even after passing a and b as keyword arguments.

Using Either One of Slash (/) or Asterisk (*) in the Function’s Parameter?

You can utilise either one depending on the type of parameters you require, whether positional-only or keyword-only. Here’s an example showing the use of only a slash (/) in a function definition.

The parameters pos1 and pos2 are positional-only parameters and pos_or_kw is a positional-or-keyword parameter.

Similarly, you can use a bare asterisk (*) also in the function definition.

Writing a Function that Accepts Positional-only Arguments

If you want to create a function or class that only accepts positional arguments, add the slash (/) at the end of the parameter list.

The function prescription() takes three arguments: med1, med2, and med3. You can see that a slash (/) is written at the end of the parameters which makes them positional-only.

You’ll get an error if you try to pass a keyword argument to the function prescription().

Writing a Function that Accepts Keyword-only Arguments

You can use a bare asterisk (*) in a function definition to make the parameters on the right side keyword-only.

If you want a function that only accepts keyword parameters, simply include a bare asterisk (*) at the beginning of the function parameter list.

The function prescription() now only accepts keyword arguments and disallows positional arguments.

Valid & Invalid Function Definitions

Here are some valid function definitions that can go well with slash and asterisk. Source

You are most likely familiar with the rules for defining positional and keyword parameters, and the function definitions mentioned above are valid in accordance with those rules.

However, the following function definitions are considered invalid because they aren’t defined as per the rules.

Once the parameter with default value has been defined, all subsequent parameters (optional for parameters after the asterisk) must be defined with the default value to avoid ambiguity in function calls.

Conclusion

The slash (/) and asterisk (*) determine how an argument should be passed when you call the function.

A slash (/) in the function definition indicates that the parameters on the left side must be treated as positional only.

A bare asterisk (*) in function definition indicates that the parameters on the right side must be treated as keyword-only.

You can use either both of them or a single in the function definition. When you are using both slash and asterisk, the slash must be inserted before the asterisk.

Resource: https://peps.python.org/pep-0570/


πŸ†Other articles you might be interested in if you liked this one

βœ…Why if __name__ == “__main__” is used in Python programs?

βœ…What is the yield keyword in Python?

βœ…Create a WebSocket server and client in Python.

βœ…How do decorators in Python work and how to create a custom decorator?

βœ…What is __getitem__ method in Python class?

βœ…The map() function in Python for mapping a function to each item in the iterable?


That’s all for now

Keep Coding✌✌