Table of Contents:
- Nonlocal variable in a nested function
- Defining a Closure Function
- When do we have a closure?
- When to use closures?
Nonlocal variable in a nested function:
Before entering the closure, it should first understand what’s happening around the functions and variables that are not non-native.
An action described in another task is called a task. Collaboration work can also be integrated circuits.
Python, non-local variables are only read-only and must state that they should be non-local (using non-local words) to hold them.
The following is an example of a temporary non-residential change.
def print_msg(msg): # This is the outer enclosing function def printer(): # This is the nested function print(msg) printer() # We execute the function # Output: Hello print_msg("Hello")
We can see that closed print () functionality that can access the service is unnecessary.
Defining a Closure Function:
In the previous example, what happened when the last line of the print_msg () function returns to the machine instead of being called? This means that the activities are described as follows.
def print_msg(msg): # This is the outer enclosing function def printer(): # This is the nested function print(msg) return printer # this got changed # Now let's try calling this function. # Output: Hello another = print_msg("Hello") another()
Fun_pressgms () is called Hello and another name. Another call (), the message is still remembered when we are done printing the print_msg () function.
The “Hello” technique will be posted on the Python number.
The cost of the surrounding animal is remembered even when the fertilizer changes or work is removed from the domain name.
Try running the Python way to see the product.
>>> del print_msg >>> another() Hello >>> print_msg("Hello") Traceback (most recent call last): ...
NameError: name ‘print_msg’ is not defined
When do we have a closure?
As seen in the previous examples, we have Python closure if the value of the reference value is within the circle.
Requirements must be completed by Python closure and summarized below.
- We need to do some practical tasks (work in the job).
- Affiliate activities should refer to the value described in the ban of activity.
- The environment must be connected.
When to use Closures?
So it is good to close?
The closure can prevent the use of world prices and provide some hidden data. You can also offer a solution to the problem of the problem.
If there are several methods (one way in most cases) to apply in the class, the closure can offer another solution to your interests. But if the number of attributes and features are bigger, please apply for a class.
Here’s a simple example of closing a better way to define the classroom as a substitute. But the preference is in each section.
def make_multiplier_of(n): def multiplier(x): return x * n return multiplier # Multiplier of 3 times3 = make_multiplier_of(3) # Multiplier of 5 times5 = make_multiplier_of(5) # Output: 27 print(times3(9)) # Output: 15 print(times5(3)) # Output: 30 print(times5(times3(2)))
In the final statement, it is better to point out that the value should be included in the closure.All substances have the properties of __closure__ function which returned to tuple tissues in the case of an operation closure. With reference to the previous example, we know
times 3 times the times the function closure.
>>> make_multiplier_of.__closure__ >>> times3.__closure__(<cell at 0x0000000002D155B8: int object at 0x000000001E39B6E0>,) Cell_contents The cells of the story have the features that cover the covered values. >>> times3.__closure__.cell_contents 3 >>> times5.__closure__.cell_contents 5