Passing an arbitrary number of arguments
- Python allows a function to collect an arbitrary number of arguments from the calling statement
pizza.py
def make_pizza(*toppings):
….”””Print the list of toppings that have been requested.”””
….print(toppings)
make_pizza(‘pepperoni’)
make_pizza(‘mushrooms’, ‘green peppers’, ‘extra cheese’)
- asterisk in the parameter name *toppings tells Python to make a tuple called toppings, containing all the values this function receives
- Python packs the arguments into a tuple, even if the function receives only one value
(‘pepperoni’,)
(‘mushrooms’, ‘green peppers’, ‘extra cheese’)
- can replace the print() call with a loop that runs through the list of toppings and describes the pizza
def make_pizza(*toppings):
…”””Summarize the pizza we are about to make.”””
….print(“\nMaking a pizza with the following toppings:”)
….for topping in toppings:
……..print(f” – {topping}”)
make_pizza(‘pepperoni’)
make_pizza(‘mushrooms’, ‘green peppers’, ‘extra cheese’)
- function responds regardless of the number of values received
Making a pizza with the following toppings:
– pepperoni
Making a pizza with the following toppings:
– mushrooms
– green peppers
– extra cheese
Mixing positional and arbitrary arguments
- parameter that accepts an arbitrary number of arguments must be placed last in the function definition
- Python matches positional and keyword arguments first and then collects any remaining arguments in the final parameter
def make_pizza(size, *toppings):
….”””Summarize the pizza we are about to make.”””
….print(f”\nMaking a {size}-inch pizza with the following toppings:”)
….for topping in toppings:
……..print(f” – {topping}”)
make_pizza(16, ‘pepperoni’)
make_pizza(12, ‘mushrooms’, ‘green peppers’, ‘extra cheese’)
- first value assigned to the parameter size, other values are stored in the tuple toppings
Making a 16-inch pizza with the following toppings:
– pepperoni
Making a 16-inch pizza with the following toppings:
– pepperoni
Making a 12-inch pizza with the following toppings:
– mushrooms
– green peppers
– extra cheese
Using arbitrary keyword arguments
- can weire functions athat accept as many key-valye pairs as the calling statement provides
- for example, building user profiles
- build_profile() function takes first and last name, but accepts an arbitrary number of keyword arguments
def build_profile(first, last, **user_info):
….”””Build a dictionary containing everything we know about a user.”””
….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)
- after the first and last name, the definition allows the user to pass as many name-value pairs as they want
- – double astersisks before the parameter **user_info cause Python to create a dictionary called user_info containing the extra name-value pairs the function receives
- within the function, you can access the key-value pairs in user_info just as you would in any dictionary
- call build_profile(), passing it the first and last name, and the two key-value pairs
- assign the returned profile to user_profile and print user_profile
{‘location’: ‘princeton’, ‘field’: ‘physics’,
‘first_name’: ‘albert’, ‘last_name’: ‘einstein’}
- returned dictionary contains first and last name and additional values
- can mix positional, keyword, and arbitrary values in many different ways when writing your own functions
- for now use simplest approach, with time you’ll learn to use the most efficient approach
- will often see parameter name **kwargs used to collect nonspecific keyword arguments
End of study session.