Python function nested instance-Python tutorial

Source: Internet
Author: User
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()()()()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.