1. What is a python closure?
In Python there is the concept of a function closure, what does this concept mean, and the instructions for viewing Wikipedia are as follows:
“
In programming languages, closures (also lexical closures or function closures) is a technique for implementing lexically Scoped name binding in languages with first-class functions. Operationally, a closure is a record storing a function[a] together with an environment:[1] a mapping associating each fre e variable of the function (variables that is used locally, but defined in a enclosing scope) with the value or storage Location to which the name is bound when the closure is created. [b] A closure-unlike a plain function-allows the function to access those captured variables through the closure ' s Referen Ce to them, even if the function is invoked outside their scope.
"--Original link: https://en.wikipedia.org/wiki/Closure_ (computer_programming)
Looks like a lot of concepts, let's talk about it in layman's terms
Let's say I have a function to ask for x^n:
Def POW (x, N): res = 1 for i in range (n): res *= x return res
(Example 1)
In a piece of code, I always use the sum of squares (for example, to find the diagonal of a rectangle), then my Code is
len2d = POW (20,2) + POW (30,2)
At this point I want the second argument to always take 2, not to write repeatedly
In another piece of code, I always use cubic and (for example, the diagonal of the cube), then my code becomes
Len3d = POW (20,3) + POW (30,3) + POW (40,3)
This time I want the second parameter to be fixed to 3
In both of these cases, the function closure is useful:
def pown (n): def POW (x): = 1 for in Range (n): # referencing peripheral function State res *= x return res return POW
Pow2 = Pown (2)
len2d = Pow2 (+) + pow2 (30)
POW3 = Pown (3)
Len3d = POW3 (+) + POW3 (+) + POW3 (40)
(Example 2)
As we can see from Example 2, pown is a peripheral function that passes in a parameter n and returns an intrinsic function. A POW is a closure function in Python that has its own execution logic and can refer to parameter n.
This is the biggest difference between the closure function and the normal function, in addition to the function execution body, the closure function also "closures" the peripheral state . Each closure function instance can be "closed" in their respective states.
2. Closures and Function objects
If you want to compare closures to C + +, it should be similar to a function object in C + +. The code for the function object to be implemented in Python is as follows:
class Pow (object): def __init__ (self, n): SELF.N =< Span style= "color: #000000;" > n def __call__ (self, x): Res = 1 for i in range (SELF.N): # Res *= x return Respow2 = Pow (2) len2d = Pow2 + pow2 (30 = Pow (3) Len3d = pow3 (+) + POW3 (+) + pow3 (+)
(Example 3)
The class in Example 3 defines a special method __call__, so its object can be directly called function, called function object. Since it is an object, it is possible to pass in parameters for saving at initialization, which is similar to the concept of closures mentioned earlier.
From this analogy, closures can be approximated as a simplified function object.
keyword: Python, closures, function objects
Python Closures and Function objects