Enter an instance of any number of parameters in the Python function
- 2021-07-18 08:32:43
- OfStack
Sometimes you don't know in advance how many arguments a function needs to accept, but Python allows a function to collect any number of arguments from a calling statement. Prefix the parameter with a *.
Let's look at a function for making pizza. It takes a lot of ingredients, but you can't determine in advance how many ingredients customers want. The following function has only one parameter * toppings, but no matter how many arguments are provided by the calling statement, this parameter captures them all:
def make_pizza(*toppings):
""" Print all ingredients ordered by customers """
print(toppings)
make_pizza('pepperoni')
make_pizza('mushroom','green peppers','extra cheese')
Implementation results:
('pepperoni')
('mushroom','green peppers','extra cheese')