Multiple formats and usage instances of functions in Python and tips
- 2020-05-09 18:46:33
- OfStack
Here are a few concepts to explain 1
- position parameter: the parameter set by position is saved implicitly with tuples. Most of the parameters we usually use are passed by position. For example, the function def func(a,b,c) is called func(1,2,3). a=1,b=2,c=3
- keyword parameters: parameters can be set through the keyword, regardless of the parameter position, implicitly save parameters with dictionary. For example, there is the function def func(a,b,c), call func(b=1,c=2,a=3), that is, a=3,b=1,c=2
General format
def func(opt_args):
...
return value
A function with a collection of location parameters
Format is as follows
def func(*params):
...
return value
usage
When using functions,*params will automatically collect the incoming parameters as a tuple without limiting the number of arguments.
The instance
def func(*params):
print params
a = [1,2,3,4]
b = 'hello'
c = 3
func(a, b, c)
The output
([1, 2, 3, 4], ' hello', 3)
A function with a collection of keyword parameters
Format is as follows
def func(**params):
...
return value
usage
**params will automatically collect the incoming parameters as a dictionary when passing parameters by keyword.
The instance
def func(**params):
print params
func(a=1, b=2, c=3)
The output
{ ' a': 1, ' c': 3, ' b': 2}
Special use of function
The default parameters
format
def func(a = 1, b = 2)
The equal sign (=) is the default value, and you can call a function without passing arguments to the default parameter.
The instance
def func(a = 1, b = 2):
print a, b
func(a=3)
The output
3 2
A function can return multiple values
format
return a, b, c
The instance
def func(a = 1, b = 2):
return a, b
print func(a=3)
The output
(3, 2)
Inline functions and closures
format
def foo() # The outer function
def bar() # Inline function
....
....
If an inline function refers to a variable of an external function (including an external function parameter) and the variable is called a free variable, then the inline function is called a closure. The referenced free variable will exist with the function 1, even if it has left the environment in which it was created.
The instance
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()
The output
6 11
The transfer function
Python1 is an object. The function structure is also an object, and the function name can be passed as a parameter
format
def bar(*param1, **param2):
....
def foo(bar, *param1, **param2):
bar(*param1, **param2)
The instance
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)
The output
def func(*params):
...
return value
8
Anonymous functions with lambda
The lambda syntax creates an anonymous function that simplifies writing and is a syntactic sugar.
Format -
def func(*params):
...
return value
9
The instance
def func(*params):
print params
a = [1,2,3,4]
b = 'hello'
c = 3
func(a, b, c)
0
The output
call foo function, result is: 7
call lambda fucntion, result is: 7