This article mainly introduces multiple formats of functions in Python and examples and tips, this article describes common formats, functions with collection location parameters, functions with collection keyword parameters, special usage of functions, embedded functions, and closures. For more information, see
Here we will explain several concepts first
-Location parameters: parameters set by location are implicitly saved as tuples. most parameters are transmitted by location. for example, function def func (a, B, c) calls func (, 3 ). that is, a = 1, B = 2, c = 3
-Keyword parameter: you can set parameters using keywords. you do not need to care about the parameter location. you can use the dictionary to save the parameters. for example, if function def func (a, B, c) is used, call func (B = 1, c = 2, a = 3), that is, a = 3, B = 1, c = 2
Normal Format
The code is as follows:
Def func (opt_args ):
...
Return value
Functions with collection location parameters
The format is as follows:
The code is as follows:
Def func (* params ):
...
Return value
Usage
When using a function, you do not need to limit the number of parameters to be passed. * params automatically collects input parameters as a tuple.
Instance
The code is as follows:
Def func (* params ):
Print params
A = [1, 2, 4]
B = 'Hello'
C = 3
Func (a, B, c)
Output
The code is as follows:
([1, 2, 3, 4], 'hello', 3)
Functions with collection keyword parameters
The format is as follows:
The code is as follows:
Def func (** params ):
...
Return value
Usage
When passing parameters by keyword, ** params automatically collects the input parameters as a dictionary.
Instance
The code is as follows:
Def func (** params ):
Print params
Func (a = 1, B = 2, c = 3)
Output
The code is as follows:
{'A': 1, 'C': 3, 'B': 2}
Special functions
Default parameters
Format
The code is as follows:
Def func (a = 1, B = 2)
Equal sign (=) is the default value. you do not need to pass the parameter to the default parameter when calling the function.
Instance
The code is as follows:
Def func (a = 1, B = 2 ):
Print a, B
Func (a = 3)
Output
The code is as follows:
3 2
The function can return multiple values.
Format
The code is as follows:
Return a, B, c
Instance
The code is as follows:
Def func (a = 1, B = 2 ):
Return a, B
Print func (a = 3)
Output
The code is as follows:
(3, 2)
Embedded functions and closures
Format
The code is as follows:
Def foo () # External function
Def bar () # embedded functions
....
....
If an embedded function references a variable of an external function (including an external function parameter) and the referenced variable is called a free variable, the nested function is called a closure. let's take a look at our professional explanation: Closure is short for Lexical Closure, a function that references free variables. This referenced free variable will exist with this function, even if it has left the environment where it was created.
Instance
The code is as follows:
Def foo (a, B ):
X = 4
Def bar ():
Return x * a + B;
Return bar
F1 = foo (1, 2)
F2 = foo (2, 3)
Print f1 (), f2 ()
Output
The code is as follows:
6 11
Transfer function
Python is an object. the syntax structure of a function is also an object. function names can be passed as parameters.
Format
The code is as follows:
Def bar (* param1, ** param2 ):
....
Def foo (bar, * param1, ** param2 ):
Bar (* param1, ** param2)
Instance
The code is as follows:
Def bar (* param1, ** param2 ):
Print param1
Print param2
Def foo (bar, * param1, ** param2 ):
Bar (* param1, ** param2)
Foo (bar, 1, 2, 3, a = 111, B = 222, c = 333)
Output
The code is as follows:
(1, 2, 3)
{'A': 111, 'C': 333, 'B': 222}
Anonymous functions and lambda
Lambda syntax can create an anonymous function, which is mainly used to simplify writing and is a syntactic sugar.
-Format
The code is as follows:
Lambda [arg1 [, arg2 ,... ArgN]: expression
Instance
The code is as follows:
Def foo (x, y ):
Return x + y
Print "call foo function, result is:", foo (3, 4)
Bar = lambda x = 2, y = 3: x + y
Print "call lambda fucntion, result is:", bar (3, 4)
Output
The code is as follows:
Call foo function, result is: 7
Call lambda fucntion, result is: 7