23 4.7. More on Defining Functions Python Tutorial, Release 3.9.5 If you don’t want the default to be shared between subsequent calls, you can write the function like this instead: def f(a, L = None ): if L is None : L =[] L .append(a) return L def parrot(voltage, state ='a stiff', action='voom',type='Norwegian Blue'): print("-- This parrot wouldn 't", action, end ='') print("if you put", voltage, "volts through it. ") print("-- Lovely plumage, the ",type) print("-- It 's", state,"!") parrot(1000 ) # 1 positional argument parrot(voltage =1000 ) # 1 keyword argument parrot(voltage =1000000 , action='VOOOOOM ') # 2 keyword arguments parrot(action ='VOOOOOM ', voltage =1000000 ) # 2 keyword arguments parrot('a million','bereft of life','jump') # 3 positional arguments parrot('a thousand', state ='pushing up the daisies ') # 1 positional, 1 keyword parrot() # required argument missing parrot(voltage =5.0 ,'dead') # non-keyword argument after a keyword argument parrot(110 , voltage =220 ) # duplicate value for the same argument parrot(actor ='John Cleese') # unknown keyword argument >>> def function(a): ... pass ... >>> function( 0, a=0) Traceback (most recent call last): File "<stdin>" , line1, in <module> TypeError: function() got multiple values for keyword argument 'a' def cheeseshop (kind,*arguments,**keywords): print("-- Do you have any ", kind,"? ") print("-- I 'm sorry, we're all out of ", kind) for arg in arguments: print(arg) (continues on next page) When a final formal parameter of the form$_{**name}$ is present, it receives a dictionary (see typesmapping) containing all keyword arguments except for those corresponding to a formal parameter. This may be combined with a formal parameter of the form$_{*name}$ (described in the next subsection) which receives a tuple containing the positional arguments beyond the formal parameter list. ($_{*name}$ must occur before$_{**name}$ .) For example, if we define a function like this: 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}$is not a valid argument for the$_{parrot}$function), and their order is not important. This also includes non-optional arguments (e.g. parrot(voltage=1000) is valid too). No argument may receive a value more than once. Here’s an example that fails due to this restriction: but all the following calls would be invalid: accepts one required argument ($_{voltage}$ ) and three optional arguments ($_{state}$, $_{action}$, and $_{type}$). This function can be called in any of the following ways: Functions can also be called using keyword arguments of the form$_{kwarg=value}$ . For instance, the following function: 4.7.2 Keyword Arguments