Writing Dynamic Python Functions: Magic of args
and kwargs
Beginner #13: How arbitrary arguments can simplify your code and boost flexibility
Table of Contents
What Are Arbitrary Arguments?
Arbitrary Positional Arguments
∘ How *args Works
∘ Examples
Arbitrary Keyword Arguments
∘ How **kwargs Works
∘ Examples
Combining *args and **kwargs
∘ Examples
∘ Key Points
Advantages of Using *args and **kwargs
Disadvantages of Using *args and **kwargs
Summary
Reference
What Are Arbitrary Arguments?
Arbitrary arguments in Python allow a function to accept a variable number of arguments, meaning that you don’t need to know how many arguments will be passed ahead of time. This is particularly useful when writing flexible functions that can handle different numbers or types of inputs.
There are two types of arbitrary arguments:
- Arbitrary Positional Arguments (using
*args
)