This article mainly introduces the Python function nesting instance. This article uses examples to explain the function nesting feature in Python. if you need it, you can refer to the Python function that can be passed as a parameter, you can also assign values to other variables (similar to Javascript, or function pointers in C/C ++ );
Similar to Javascript, Python supports function nesting. the application mode of Javascript nested functions is applicable to Python;
The code is as follows:
>>> Def multiplier (factor ):
... Def multiple (number ):
... Return number * factor
... Return multiple
...
>>>
>>> Multiplier (3) (3)
9
>>> Multiplier (4) (3)
The closure feature is closely related to nested functions. for example:
The code is as follows:
>>> Def test ():
... A = {'name': 'wyj '}
... Def f ():
... Return a ['name']
... Return f
...
>>> Def test ():
... A = {'name': 'wyj '}
... Def f ():
... Return a ['name']
... Return a, f
...
>>> A, f = test ()
>>> F ()
'Wyj'
>>> A ['name'] = 'ljq'
>>> F ()
'Ljq'
Example 3
#! /usr/bin/pythondef yao1(a=1,b=32):def yao2(c=54):def yao3(d=3):def yao4(e=10):return a+b+c+d+ereturn yao4return yao3return yao2print yao1()()()()