Python When you define a function You don't have to specify The type of parameter , Argument types passed entirely by the caller and Python The interpreter's understanding and inference determine .
Next , This article will introduce the parameter types of the following four functions :
Positional arguments : Actual and formal parameters when calling a function The sequence must be strictly consistent , And arguments and formal parameters The quantity must be the same .
The following format :
def showArgs(a, b, c):
print(a, b, c)
show(1, 2, 3) # 1 2 3
show(1, 2) # TypeError: showArgs() missing 1 required positional argument: 'c'
show(1, 2, 3, 4) # TypeError: showArgs() takes from 1 to 3 positional arguments but 4 were given
Default parameter : When calling a parameter with a default value , You can not assign a value to a parameter that has a default value ( If there is , will Use the passed value ), That is, different from the above position parameters , Parameter passing can be missing , But you can't pass on more .
How to use it : namely , stay When defining a function :def demo(a=1, b=2)
A default value is given when filling in parameters .
Let's take an example :
def showArgs(a, b=3, c=2):
print(a, b, c)
showArgs(1, 2, 3) # 1 2 3
showArgs(1, 2) # 1 2 2
showArgs(1, 2, 3, 4) # TypeError: showArgs() takes from 1 to 3 positional arguments but 4 were given
matters needing attention :
Use , The key parameters , The order of arguments and formal parameters can be different , However, if the parameter has no default value , The number of parameters should also be consistent .
How to use it : stay When you call a function : demo(a=1, b=2)
To specify the value of the parameter .
Let's take an example :
def showArgs(a, b, c):
print(a, b, c)
showArgs(1, 2, c=3) # 1 2 3
showArgs(a=1, c=22, b=33) # 1 33 22
matters needing attention :
Wrong situation :
showArgs(1, 22, b=33)
It will report this error :TypeError: showArgs() got multiple values for argument 'b'
, Because though b
Key parameters are used , But the first two are positional parameters , Through position parameters ,b
It has been assigned
showArgs(a=11, c=222, 333)
It will report this error :SyntaxError: positional argument follows keyword argument
, Key parameters cannot be followed by positional parameters
Variable length parameter : That is, the parameter length is not fixed
It has the following two forms :
*parameter
: Receive multiple Location parameter And put it in Tuples in **parameter
: Receive multiple The key parameters And put it in Dictionaries in Among them parameter
You can customize the name you want
Must receive Positional arguments !!!
Its usage is as follows :
def showArgs(*args):
print(args)
showArgs(1, 2, 3) # (1, 2, 3)
showArgs(7, 8) # (7, 8)
Must receive The key parameters !!!
Its usage is as follows :
def showArgs(**args):
print(args)
showArgs(a=1, b=2, c=3) # {'a': 1, 'b': 2, 'c': 3}
showArgs(a=7, b=8) # {'a': 7, 'b': 8}
Wanyeji Faint thunder , Cloud
Make a little progress every d