What does double star/asterisk and star/asterisk do for parameters?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In the world of programming, particularly in Python, the use of asterisks (*) and double asterisks (**) in function parameters serves a unique and useful purpose—allowing for flexible argument handling. This functionality is fundamental when dealing with functions that need to handle a large number of arguments or with arguments that may not all be known when writing the function.
The Single Asterisk: *
The single asterisk * is used in function definitions to allow for variable-length positional arguments. It collects all the positional arguments beyond the ones listed into a tuple. This is what is often referred to as "packing".
Using * for Variable-Length Positional Arguments
When a function parameter is prefixed with *, the function can accept any number of positional arguments. These arguments are packed into a tuple that the function can then process:
In this example, you can call print_names() with any number of arguments:
Unpacking with *
The * operator can also be used to unpack iterable objects into individual elements when passing arguments to a function:
In this function call, the tuple numbers is unpacked into three positional arguments using the asterisk.
The Double Asterisk: **
The double asterisk ** is similarly invaluable when dealing with functions, but instead of positional arguments, it pertains to keyword arguments. It allows for packing and unpacking of dictionaries.
Using ** for Variable-Length Keyword Arguments
In a function definition, ** collects further keyword arguments into a dictionary:
This function can be called with any number of keyword arguments:
Unpacking with **
Just as * unpacks a list or tuple, ** is used to unpack a dictionary into keyword arguments:
Here, student_info is a dictionary that is unpacked into the name and age parameters.
Differences and Common Use-Cases
| Feature | * (Single Asterisk) | ** (Double Asterisk) |
| Purpose | Gathers positional arguments into a tuple | Gathers keyword arguments into a dictionary |
| Use Case | Flexible argument counts when the exact number isn’t known. | Use when functions need to accept optional key-value pair arguments. |
| Unpacking | Use *args to unpack sequences | Use **kwargs to unpack dictionary-like objects |
| Example Usage | def foo(*args):
Used to iterate over arguments | def bar(**kwargs):
Handle named parameters as dictionary |
Additional Considerations
Combining * and **
Functions can combine both *args and **kwargs to allow for handling positional as well as keyword arguments:
Order of Parameters
When defining a function with both kinds of arguments, the order is critical: first, define standard positional arguments, followed by *args, standard keyword arguments, and finally **kwargs.
Language Compatibility
While these concepts are especially prevalent in Python, many other programming languages offer similar functionality with variadic arguments. However, the specific syntax and semantics can differ significantly.
Conclusion
Understanding the use of * and ** in function parameters enhances the flexibility and robustness of your code. They enable functions to process an indeterminate number of inputs, making these functions highly reusable across various contexts. Once you become comfortable with these concepts, you will likely find yourself writing cleaner and more efficient code.

