Collection function : Parameter the user likes to pass several parameters of the feature , Definition : Add an asterisk to the name of the parameter * Can be .
>>> def myfunc(*args):
print(" Yes {} Parameters .".format(len(args)))
print(" The second parameter is :{}.".format(args[1]))
>>> myfunc(' Little turtle ', ' No two in yes ')
Yes 2 Parameters .
The second parameter is : No two in yes .
>>> myfunc(1, 2, 3, 4, 5)
Yes 5 Parameters .
The second parameter is :2.
>>> def myfunc(*args):
print(args)
>>> myfunc(1, 2, 3, 4, 5)
(1, 2, 3, 4, 5) // The essence is tuple , Tuples have the ability to package and unpack
>>> def myfunc():
return 1, 2, 3 // Return multiple values
>>> myfunc()
(1, 2, 3)
>>> x, y, z = myfunc() // Unpack
>>> x
1
>>> y
2
>>> z
3
Function parameters are passed in the same way as tuples , adopt * Packaging operation can be realized , Pack multiple parameters into a tuple
>>> def myfunc(*args):
print(type(args))
>>> myfunc(1, 2, 3, 4)
<class 'tuple'>
notes : There are other parameters after the collected parameters , This parameter can only be specified with keyword parameters .
>>> def myfunc(*args, a, b):
print(args, a, b)
>>> myfunc(1, 2, 3, 4, 5)
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
myfunc(1, 2, 3, 4, 5)
TypeError: myfunc() missing 2 required keyword-only arguments: 'a' and 'b'
>>> myfunc(1, 2, 3, a=4, b=5)
(1, 2, 3) 4 5
The asterisk is actually an anonymous collection parameter
>>> def abc(a, *, b, c):
print(a, b, c)
>>> abc(1, 2, 3)
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
abc(1, 2, 3)
TypeError: abc() takes 1 positional argument but 3 were given
>>> abc(1, b=2, c=3)
1 2 3
Collecting parameters can pack multiple parameters into tuples , You can also package parameters as dictionaries , It can be packaged into a dictionary by two asterisks .
>>> def myfunc(**kwargs):
print(kwargs)
>>> myfunc(a=1, b=2, c=3) // Dictionary elements are key value pairs , The keyword has values on both sides , And there is an equal sign
{'a': 1, 'b': 2, 'c': 3}
Mix it up , There are both tuples and dictionaries
>>> def myfunc(a, *b, **c):
print(a, b, c)
>>> myfunc(1,2,3,4,x=5,y=6)
1 (2, 3, 4) {'x': 5, 'y': 6}
The functions that have both dictionaries and tuples are format()
>>> help(str.format)
Help on method_descriptor:
format(...)
S.format(*args, **kwargs) -> str
Return a formatted version of S, using substitutions from args and kwargs.
The substitutions are identified by braces ('{' and '}').
Unpack parameters : Using asterisks on formal parameters is called parameter packing , Use... On arguments , Then unpack .
>>> args = (1, 2, 3, 4)
>>> def myfunc(a, b, c, d):
print(a, b, c, d)
>>> myfunc(args)
Traceback (most recent call last):
File "<pyshell#42>", line 1, in <module>
myfunc(args)
TypeError: myfunc() missing 3 required positional arguments: 'b', 'c', and 'd'
>>> myfunc(*args)
1 2 3 4
>>> kwargs = {'a':1, 'b':2, 'c':3, 'd':4}
>>> myfunc(kwargs)
Traceback (most recent call last):
File "<pyshell#48>", line 1, in <module>
myfunc(kwargs)
TypeError: myfunc() missing 3 required positional arguments: 'b', 'c', and 'd'
>>> myfunc(**kwargs)
1 2 3 4