Pass any number of real parameters
There are times when you don't know in advance how many real parameters a function needs to accept. The good thing is that python allows functions to collect any number of real parameters from the calling statement.
Let's take a practical example, let's say, we're making sizzling mixed grain pancakes (mmmm, that sounds good~), it's possible to add a lot of favorite toppings, but we don't know what the customers who come in want to add.
Take a look at a piece of code where the function has only one formal parameter *toppings, but no matter how many real parameters are provided by the calling statement, this formal parameter takes them all in:
def make_jianbing(*toppings): print(toppings) make_jianbing('regou') make_jianbing('weilong','liang ge eggs','regou')
('regou',) ('weilong', 'liang ge eggs', 'regou')
The asterisk in the formal parameter name *toppings lets python create an empty tuple called toppings and encapsulate all the values it receives into that tuple.
The print statement within the function body demonstrates that python can handle calling a function with one value as well as three values by generating output.
It handles different calls in a similar way.
Note that python encapsulates real parameters into a tuple, even if the function receives only one value.
to continue expanding on it:
def make_jianbing(*toppings): print("\nMakeing a jianbing with the following toppings: ") for topping in toppings: print("-" + topping) make_jianbing('regou') make_jianbing('weilong','liang ge eggs','regou')
Makeing a jianbing with the following toppings: -regou Makeing a jianbing with the following toppings: -weilong -liang ge eggs -regou
This syntax works well regardless of the number of real parameters the function receives.
Combined use of positional and arbitrary number of real parameters
If you want a function to accept different types of real parameters, you must place the formal parameter that accepts any number of real parameters at the end of the function definition.
python matches positional and keyword real parameters first, then collects all the remaining real parameters into the last formal parameter.
Like what?
In the above example, it is possible that a customer who needs more than one pancake will need a real parameter for the quantity, and that formal parameter must be placed in front of the formal parameter *toppings:
def make_jianbing(count,*toppings): print("\nMaking a " + str(count) + "-ge jianbing with the following toppings: ") for topping in toppings: print("- " + topping) make_jianbing(2,'kaochang') make_jianbing(4,'weilong','shuanghuandan','kaochang')
Making a 2-ge jianbing with the following toppings: - kaochang Making a 4-ge jianbing with the following toppings: - weilong - shuanghuandan - kaochang
The above function defines that python stores the first value it receives into count and stores all other values in toppings.
Use any number of keyword real parameters
Sometimes, yeah, it's necessary to accept an arbitrary number of real parameters without knowing in advance what kind of information will be passed to the function.
When such a requirement is encountered, the function can be written to accept any number of key-value pairs ----- as many as are provided by the calling statement.
Give an example of creating a user profile:
For example, it will receive information about the user, but it is not known what kind of information it will be, such as in the following example, the function build_profile() accepts the first name and last name, and also accepts any number of keyword real parameters:
def build_profile(first, last, **user_info): """ Create a dictionary that contains everything we know about the user """ profile = {} profile['first_name'] = first profile['last_name'] = last for key,value in user_info.items(): profile[key] = value return profile user_profile = build_profile('albert' , 'einstenin' , location='princeton', field = 'physics') print(user_profile)
Implementation results:
{'field': 'physics', 'first_name': 'albert', 'location': 'princeton', 'last_name': 'einstenin'}
The definition of the build_profile() function requires a first and last name, while allowing the user to supply as many name-value pairs as desired.
The two asterisks in the formal parameter **user_info let python create an empty dictionary called user_info and encapsulate all name-value pairs received into this dictionary.
In this function, name-value pairs in user_info can be accessed like any other dictionary.
The returned dictionary includes the user's first and last name, as well as the place of study and the major studied. The function handles this function correctly no matter how many additional key-value pairs are supplied when it is called.
Functions can be written with a mix of positional real parameters, keyword real parameters, and any number of real parameters in a variety of ways.
Exercise:
def make_guandongzhu(*foods): print(foods) for food in foods: print("\nYour select guan dong zhu include: " + food) make_guandongzhu('luobo','moyusi','haidian') make_guandongzhu('xianggujirouwan','yuzixianbei') make_guandongzhu('yudoufu','doupi','eggs')
('luobo', 'moyusi', 'haidian') Your select guan dong zhu include: luobo Your select guan dong zhu include: moyusi Your select guan dong zhu include: haidian ('xianggujirouwan', 'yuzixianbei') Your select guan dong zhu include: xianggujirouwan Your select guan dong zhu include: yuzixianbei ('yudoufu', 'doupi', 'eggs') Your select guan dong zhu include: yudoufu Your select guan dong zhu include: doupi Your select guan dong zhu include: eggs
Exercise:
def cars_info(changjia,xinghao,**infos): cars = {} cars['producer'] = changjia cars['car'] = xinghao for key,value in (): cars[key] = key cars[value] = value return cars car_info = cars_info('beiqi','hongqi',ranliao='qiyou',money='pianyi') print(car_info)
{'car': 'hongqi', 'ranliao': 'ranliao', 'pianyi': 'pianyi', 'producer': 'beiqi', 'qiyou': 'qiyou', 'money': 'money'}
Note that the function arguments are two asterisks
summarize
The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.