The # keyword parameter # variable parameter allows you to pass in 0 or any parameter, which is automatically assembled as a tuple# when called, and the keyword parameter allows 0 or any parameter with parameter names to be passed in. These keyword parameters are automatically assembled inside the function as a Dictdef person (name, age, **kw): print (' Name: ', name, ' Age: ', age, ' other ', kw) the # function person accepts the keyword parameter kw in addition to the required parameter name and age. # when calling this function, you can pass in only the required parameters person ("Yel", 23) # can also pass in any number of keyword parameters person (' Bob ', 35, city= ' Beijing ') ) person (' Adam ', 45, gender= ' M ', job= ' Engineer ') # ----------------------------------------- ------------------------------------------------------# keyword parameter can extend function # For example, in the person function, We are guaranteed to receive both the name and the age parameters, but if the caller is willing to provide more arguments, we can also receive # imagine being doing a user registration function, in addition to the user name and ages are required, the others are optional, Use keyword parameters to define this function to meet the requirements of registration # -------------------------------------------------------------------------------- ---------------# is similar to variable parameters, you can also assemble a dict, and then convert the dict into a keyword parameter to extra = {' city ': ' Beijing ', ' job ': ' Engineer '}person (' Jack ', 24, city=extra[' city '], job=extra[' job ']) # the above complex call can be simplified notation # **extra to extra this dict all key-value with the keyword parameter passed to the **KW parameter of the function, KW will get a dict, Note that the dict obtained by KW is a copy of extra, and the changes to KW do not affect the Extraperson outside the function (' Jack ', 24, **extra)
Python---function---keyword parameters