Python function Introduction 3: python function Introduction
1. Non-fixed number of arguments-parameter group, * args. The result is displayed in the form of tuples.
Def func (* args): print (args) func (, 5) # first call method, multiple real parameters func (* [, 5]) # The second method is called in the form * [].
# Running result (1, 2, 3, 4, 5) (1, 2, 3, 4, 5)
2. Fixed Number of real parameters and parameter groups. Combination of the two parameters (a, * args)
Def func2 (x, * args): print (x, args) func2 ('A', 1, 2, 3, 4) # deduct the fixed parameters. All other real parameters are included in the parameter group, list in the form of tuples # result a (1, 2, 3, 4)
3. ** kwargs converts a real parameter (key = value) into a dictionary.
Def func3 (** kwargs): print (kwargs) print (kwargs ['name']) print (kwargs ['age']) func3 (name = 'frank ', age = 26) # result: {'name': 'frank', 'age': 26} Frank26
4. args, * args, ** kwargs
Def func5 (name, * args, ** kwargs): print (name, args, kwargs) func5 ('Alex ', 1, 3, 'teacher', age = 18, sex = 'M') # result Alex (1, 3, 'teacher') {'age': 18, 'sex': 'M '}
5. High-order functions: a variable can point to a function. If a function parameter can accept a variable, one function can take another function as a parameter.
Def func (a, B, f): return f (a) + f (B) B = func (1,-1, abs) print (B) # result 2