Functions can be called in the form of keyword arguments, like keyword =value. For example, the following function: Def parrot (Voltage, state='a stiff', action='Voom', type='Norwegian Blue'): Print"--This parrot wouldn ' t", action, print"If you put", Voltage,"volts through it."Print"--lovely plumage, the", type print"--It ' s", state,"!"accepts a required parameter (voltage) and three optional parameters (State, action, and type). Can be called with any of the following methods: Parrot ( +) #1positional Argumentparrot (voltage= +) #1keyword Argumentparrot (voltage=1000000, action='Vooooom') #2keyword Argumentsparrot (action='Vooooom', voltage=1000000) #2keyword Argumentsparrot ('a million','Bereft of life',' Jump') #3positional Argumentsparrot ('a thousand', state='pushing up the daisies') #1Positional,1keyword However, the following calls are not valid: Parrot () # required argument Missingparrot (voltage=5.0,'dead') # non-keyword argument after a keyword Argumentparrot ( the, voltage= -) # Duplicate value forThe same Argumentparrot (actor='John Cleese'# Unknown keyword argument Typically, each keyword in the argument list must come from a formal parameter, each with a corresponding keyword. It is not important to have a default value for formal parameters. The actual parameter cannot be assigned multiple values at one time-the formal parameter cannot use both the location and the keyword binding values in the same call. Here is an example of a failure condition that occurs under this constraint:>>>def function (a): ... pass ...>>> function (0, a=0) Traceback (most recent): File"<stdin>", line1,inch?typeerror:function () got multiple values forKeyword argument'a'introduce a shape like**name parameter, it receives a dictionary (see typesmapping), which contains all the keyword parameters that do not appear in the formal parameter list. This may also be combined with a formal parameter such as *name (detailed in the next section), which receives a tuple (described in detail in the next sections) and contains all the parameter values that are not present in the formal parameter list. (*name must be in * *name appears before) For example, we define a function like this: Def cheeseshop (Kind,*arguments, * *keywords): print"-- don't have any", Kind,"?"Print"--I ' m Sorry, we ' re all out of", Kind forArgincharguments:print arg print"-"* +Keys=Sorted (Keywords.keys ()) forkwinchkeys:print kw,":", keywords[kw] It can be called like this: Cheeseshop ("Limburger","It ' s very runny, sir.", "It ' s really very, very runny, sir.", shopkeeper='Michael Palin', Client="John Cleese", Sketch="Cheese Shop Sketch"of course, it will print as follows:--Do Limburger?--I'm Sorry, we'Re All outof Limburgerit's very runny, sir.It's really very, very runny, sir.----------------------------------------Client:john cleeseshopkeeper:michael Palinsketch:cheese Shop sketch Note the sort () method is called before printing the contents of the keyword parameter dictionary. Otherwise, the order in which the parameters are printed is undefined.
[Python function learning Article] keyword parameters