AlgoMaster Logo

*args and **kwargs

Last Updated: January 3, 2026

6 min read

When you start working with functions in Python, you quickly realize that they can be incredibly flexible. One of the most powerful features that adds to this flexibility is the ability to handle variable numbers of arguments using *args and **kwargs.

These constructs allow you to write functions that can accept an arbitrary number of positional and keyword arguments, respectively. This isn't just a fancy trick; it opens up a lot of possibilities in managing data and functionality.

Understanding *args

At its core, *args lets you pass a variable number of positional arguments to a function. When you prefix a parameter with an asterisk (*), Python collects any additional positional arguments into a tuple. This is particularly useful when you're not sure how many arguments might be passed to your function.

Basic Usage

Let’s start with a simple example of how *args works:

Here, summation can take any number of numbers, and it will return their sum. You can see how *args lets us call the function with varying numbers of arguments without needing to define each one explicitly.

Real-World Examples

Imagine you are building a logging function that can accept different types of messages. Using *args, you can handle this easily:

This function logs each message, and you can pass as many messages as you'd like without modifying the function's definition.

Edge Cases with *args

While *args is powerful, it can lead to confusion if misused. For instance, if you have default arguments, remember that *args must come after them:

If you attempt to place *args before the default parameters, Python will raise a SyntaxError. Always keep this in mind when designing your functions.

Understanding **kwargs

On the flip side, **kwargs allows you to pass a variable number of keyword arguments. When you prefix a parameter with two asterisks (**), Python collects these in a dictionary. This is useful for passing a variety of named arguments without having to define them all explicitly.

Basic Usage

Let’s start with a simple example of using **kwargs:

In this example, print_student_info can take any number of keyword arguments, which are stored in the kwargs dictionary.

Real-World Examples

Imagine you’re creating a function to configure a user profile. Using **kwargs, you can allow users to set various attributes:

This gives you a flexible way to configure profiles with varying attributes, allowing your function to be dynamic and adaptable.

Merging args and *kwargs

You can combine *args and **kwargs in the same function. However, the order matters: you need to list *args first, followed by **kwargs:

This function accepts two required positional arguments, collects extra positional arguments into args, and named arguments into kwargs. The output shows how everything is organized:

Practical Applications

Building APIs

In real-world scenarios, especially when building APIs or handling user input, *args and **kwargs become invaluable. They allow you to create functions that can handle optional parameters gracefully. For instance, consider a function that sends HTTP requests:

This function can accommodate any additional positional or keyword arguments that requests.get might accept, making it super flexible.

Data Processing Functions

Another common use case is in data processing. You might have a function that processes different types of datasets where the structure may vary:

This structure lets you easily extend functionality without changing the function signature whenever new data types or options are introduced.

Common Gotchas with args and *kwargs

While *args and **kwargs are powerful, there are a few common pitfalls to watch out for:

  • Order of Arguments: Always remember that if you are using both *args and **kwargs, *args must come before **kwargs in your function definition.
  • Using with Other Parameters: You can mix them with regular parameters, but they need to be in the correct order: normal parameters, then *args, and finally **kwargs.
  • Overwriting Variables: If you use the same name for a keyword argument in **kwargs and a standard parameter, the keyword argument will take precedence, which might lead to unexpected behavior.
  • Unpacking: You can also unpack *args and **kwargs into functions. This allows you to pass arguments from one function to another seamlessly:

Conclusion

Understanding *args and **kwargs can significantly enhance your ability to write flexible and reusable functions in Python. They allow your functions to adapt to various inputs without having to redefine them for each new situation. You can create powerful APIs, data-processing functions, and more with the right use of these constructs.

Now that you understand how to effectively manage variable arguments with *args and **kwargs, you are ready to explore Keyword Arguments in more detail.

In the next chapter, we will dive deeper into how keyword arguments work, including their implications and practical uses in function definitions and calls.