Functions can also be called using keyword arguments of the form kwarg=value. For instance, the following function:
DefParrot(Voltage,State=' A stiff ',Action=' Voom ',Type= ' Norwegian Blue ' ): print ( "--this parrot wouldn ' t" actionend< Span class= "o" >= ") print (" if You put "voltage" volts through it. " ) print ( "-lovely plumage," type print (state "!" ) /span>
Accepts one required argument (voltage) and three optional arguments (State, action, and ty PE). This function can is called in any of the following ways:
Parrot(1000)# 1 Positional argumentParrot(Voltage=1000)# 1 keyword argumentParrot(Voltage=1000000,Action= ' vooooom ' ) # 2 keyword argumentsparrot (action= ' vooooom ' voltage=1000000) # 2 keyword Argumentsparrot ( ' A million ' " Bereft of life ' ' jump ' # 3 positional arguments< span class= "n" >parrot ( ' a thousand ' state = ' pushing up the Daisies ' ) # 1 positional, 1 keyword
But all the following calls would is invalid:
Parrot() # required argument missingparrot(voltage=5.0' dead '# Non-keyword Argument after a keyword argumentparrot(voltage= +Duplicate value for the same ArgumentParrot(actor=' John Cleese '# unknown keyword argument
In a function call, keyword arguments must follow positional arguments. All the keyword arguments passed must match one of the arguments accepted by the function (e.g. actor was not a VA Lid argument for the parrot function), and their order was not important.
1 The keyword parameter is after the position parameter;
2 The order of the keyword parameters can be arbitrary;
Define Functions:keyword Arguments