Defining mutable parameters
If you want a function to accept any of the arguments, we can define a variable parameter:
DEF fn (*args): print args
#coding =gbkdef Kebian (*arg): Return Argprint (Kebian (1)) Print (Kebian (' Str ')) print (Kebian ([+)]) print (Kebian ({' Right ': 1, ' Left ': 2})
There is an * number in front of the variable parameter, we can pass 0, one or more parameters to the variable parameter:
>>> fn () () >>> fn (' a ') (' a ',) >>> fn (' A ', ' B ') (' A ', ' B ') >>> fn (' A ', ' B ', ' C ') (' A ', ' B ') , ' C ')
Mutable parameters are also not very mysterious, the Python interpreter will assemble a set of parameters passed into a tuple to the variable parameters, so, inside the function, the variable args directly as a tuple is good.
The purpose of defining mutable parameters is also to simplify the invocation. Suppose we want to calculate an arbitrary number of averages, we can define a variable parameter:
def average (*args): ...
In this way, you can write this at the time of the call:
>>> average () 0>>> average (1, 2) 1.5>>> average (1, 2, 2, 3, 4) 2.4
Python variable Parameters