Python- Day 26- FUNCTIONS-Passing an Arbitrary Number of Arguments
Collecting Arbitrary Positional Arguments:
Sometimes, you won’t know ahead of time how many arguments a function will need.
Python allows you to collect an arbitrary number of positional arguments using an asterisk *
before the parameter name. This packs all the additional arguments into a tuple.
Example: A pizza-ordering function that accepts any number of toppings:
def make_pizza(*toppings):
print(toppings)
make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')
>>
('pepperoni',)
('mushrooms', 'green peppers', 'extra cheese')
In this example, the *toppings
parameter collects all toppings as a tuple. Even if there is only one argument, it still packs it into a tuple.
Iterating Through Arbitrary Arguments:
Instead of directly printing the tuple, you can loop through the collected arguments to perform actions on them.
def make_pizza(*toppings):
print("\nMaking a pizza with the following toppings:")
for topping in toppings:
print(f"- {topping}")
make_pizza('pepperoni')
# Output: Making a pizza with the following toppings: - pepperoni
make_pizza('mushrooms', 'green peppers', 'extra cheese')
# Output: Making a pizza with the following toppings: - mushrooms - green peppers - extra cheese
Mixing Positional and Arbitrary Arguments:
You might need to accept both fixed and arbitrary arguments.
Positional arguments should come first, and the arbitrary arguments should be placed at the end of the function definition.
Example: Adding a size argument to the pizza function:
def make_pizza(size, *toppings):
print(f"\nMaking a {size}-inch pizza with the following toppings:")
for topping in toppings:
print(f"- {topping}")
make_pizza(16, 'pepperoni')
# Output: Making a 16-inch pizza with the following toppings: - pepperoni
make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
# Output: Making a 12-inch pizza with the following toppings: - mushrooms - green peppers - extra cheese
Here, the first argument is treated as the pizza size, while the remaining arguments are packed into the *toppings
tuple.
Using Arbitrary Keyword Arguments:
Sometimes, you won’t know what kind of information (key-value pairs) will be passed to the function.
Use double asterisks **
to collect an arbitrary number of keyword arguments. Python creates a dictionary with these key-value pairs.
Example: A function that builds a user profile:
def build_profile(first, last, **user_info):
user_info['first_name'] = first
user_info['last_name'] = last
return user_info
user_profile = build_profile('albert', 'einstein', location='princeton', field='physics')
print(user_profile)
# Output: {'location': 'princeton', 'field': 'physics', 'first_name': 'albert', 'last_name': 'einstein'}
The **user_info
parameter collects additional key-value pairs into a dictionary. The function always accepts a first and last name, but other information like location and field is optional.
Combining Positional, Keyword, and Arbitrary Arguments
You can mix positional, keyword, and arbitrary arguments in different ways to create flexible functions.
This is useful in various situations like handling different types of user inputs or parameters.
Common Usage: In many Python codes, you’ll see *args
(arbitrary positional arguments) and **kwargs
(arbitrary keyword arguments) to collect flexible sets of parameters.
If you found this guide helpful then do click on 👏 the button.
Follow for more Learning like this 😊
If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!