Parameter Unpacking: *args
, **kwargs
In Python, functions can accept a variable number of arguments using *args
and **kwargs
. These allow for more flexible and dynamic function definitions.
1. Understanding *args
(Non-keyworded Variable Arguments)
*args
allows a function to accept any number of positional arguments.- It collects extra positional arguments into a tuple.
Example:
Output: *args
is just a convention. You can name it anything.- It must be placed after all regular positional parameters in a function definition.
2. Understanding **kwargs
(Keyworded Variable Arguments)
**kwargs
allows a function to accept any number of keyword arguments.- It collects them into a dictionary.
Example:
Output: **kwargs
is also a convention and can be renamed.- It should be the last parameter in the function signature if used with others.
3. Combining *args
and **kwargs
You can combine both in one function. Just remember: *args
must come before **kwargs
.
Example:
Output:
Name: Abhijit
Additional Info (Positional Arguments):
30
Engineer
Additional Info (Keyword Arguments):
country: India
city: Mumbai
4. Using *args
and **kwargs
in Function Calls
You can unpack arguments from lists/tuples and dictionaries using *
and **
during function calls.
Example:
Output:
Hello, my name is Alice and I am 30 years old.
Hello, my name is Bob and I am 25 years old.
Hello, my name is Charlie and I am 35 years old.
5. Using *args
and **kwargs
with Default Parameters
You can mix *args
and **kwargs
with regular/default parameters.
Example:
6. Summary
*args
: Collects extra positional arguments into a tuple.**kwargs
: Collects extra keyword arguments into a dictionary.- You can use both together in function definitions and calls.
- They provide flexibility and scalability to function definitions.