Python function advanced

Source: Internet
Author: User

1. Nested functions

The function Body contains a complete function, which can also be called a sub-function

---------------------------- nested function scope problems ---------------------------

Scope :

The inner function can access the scope of the outer function.

An external function cannot access the scope of an intrinsic function.

# CODING:GBK def Test (m,n):         def test1 ():         Print ('hehe')         return m+n        return  test1 ()print(Test (8,9))

2. anonymous function

# CODING:GBK  Lambda x,y:x+yprint(func_name)print(func_name)

Equivalent to

def add (x, y):
Return X+y

The lambda Colon is preceded by a parameter, followed by a return result
Function name () represents the execution of a function
Difference:
Lambda returns a function name

The normal function returns the result

A = {'Lilei': 94,'Lily': 80,'Lucy': 75,'Hanmeimei': 90}a.items () dict_items ([('Lilei', 94), ('Lily', 80), ('Lucy', 75), ('Hanmeimei', 90)]) list (A.items ()) [('Lilei', 94), ('Lily', 80), ('Lucy', 75), ('Hanmeimei', 90)]sorted (A.items (), Key=LambdaX:x[1],reverse=True) [('Lilei', 94), ('Hanmeimei', 90), ('Lily', 80), ('Lucy', 75)]

Python function advanced

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.