Learning---Anonymous functions and closures in Python learning

Source: Internet
Author: User
Tags closure

1.1. Anonymous Functions

Anonymous naming rules for name functions : identified by the LAMDBA keyword, the left side of the colon (:) indicates the parameter (a, b) that the function receives, and the right side of the colon (:) represents the function's return value (A+B). Because LAMDBA does not need to be named when it is created, it is called an anonymous function

Add = lambda A, b:a * bprint (Add (2, 3))    #  6

1.2. functional programming and imperative programming

Our common programming paradigm is command-programmed (imperative programming), functional Programming

Common object-oriented programming is also a kind of imperative programming.

Imperative Programming is an abstraction for computer hardware , with variables (corresponding to storage units), assignment Statements (fetch, store instructions), expressions (memory references and arithmetic operations), and Control statement (jump instruction), in a word, the imperative program is a Von Neumann sequence of instructions .
Functional Programming is a mathematical-oriented abstraction that describes computation as an expression evaluation , in a word, a functional program is an expression .

Functional programming concerns the mapping of data, imperative programming cares about the steps to solve the problem

1.3. Closed Package

definition: If a reference is made to a variable in an external scope (but not at the global scope) in an intrinsic function, then the intrinsic function is considered a closure (closure).

simply speaking , it's closures = function blocks (intrinsic functions) + (external) environment variables when defining intrinsic functions

# DEF outer (x):  # When this x variable is passed in as a parameter, it is equivalent to the external variable of the intrinsic Function Def outer ():    x =    def inner ():        print (x)    return inner# yy = outer (+) # yy () The result of the execution is 100, the same reason XX = outer ()   # outer () has performed the completion of XX ()           #  Why outer has been executed, But inner () can also fetch the x=10 value?               # Closures (closure)! The inner of return in outer is a closure function with an environment variable of x. Outer () ()      # 19# inner ()      #  name ' inner ' is not defined  scope is local# print (x)     #  name ' x ' are not Defined   Scope Local

Learning---Anonymous functions and closures in Python learning

Related Article

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.