Skip to main content

Functions the Pythonic Way

Arguments: *args and **kwargs

0:00
LearnStep 1/3

Flexible Function Signatures

Python functions are remarkably flexible. Beyond standard positional and keyword arguments, you can design functions that accept an arbitrary number of inputs or enforce strict calling conventions. This lesson covers the powerful tools *args, **kwargs, and the special separators * and /.

1. Variable Positional Arguments (*args)

The *args parameter allows a function to accept any number of positional arguments. Inside the function, args becomes a tuple containing all the extra arguments.

python

2. Variable Keyword Arguments (**kwargs)

Similarly, **kwargs allows a function to accept any number of keyword arguments. Inside the function, kwargs is a dictionary.

python

3. Keyword-Only Arguments

Sometimes you want to force a user to use the parameter name when calling a function, often for clarity with boolean flags or configuration options. You can achieve this by placing arguments after a bare * or after *args.

python

4. Positional-Only Arguments

Introduced in Python 3.8, the / separator allows you to define arguments that cannot be passed as keywords. This is useful when the argument name doesn't matter or for API consistency with C modules.

python