Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Implementation of Closure | Closure
Mastering Python: Closures and Decorators

bookImplementation of Closure

Let's consider the example of closure:

1234567891011121314151617
def outer(argument): def inner(): nonlocal argument argument += 5 return argument return inner variable1 = outer(11) variable2 = outer(9) print("First variable1 returns", variable1()) print("Second variable1 returns", variable1()) print("First variable2 returns", variable2()) print("Second variable2 returns", variable2())
copy

We can operate with the enclosed data: the nonlocal keyword provides the ability to change argument (similar to the global keyword).

Steps to create closure

Step 1:
Create the outer function with its local scope.

def outer():
    a = 4

Step 2:
Create an inner function with local scope defined inside the outer function.

def outer():
    a = 4
    
    def inner():

Step 3:
Access the non-local (outer scope) data from within the inner local scope.

def outer():
    a = 4
    
    def inner():
        nonlocal a

Step 4:
Return the inner function and assign it to the variable.

def outer():
    a = 4
    
    def inner():
        nonlocal a

    return inner

variable = outer()

Step 5:
The local scope of the outer function is removed once it has finished executing.

#def outer():
#    a = 4
#    
    def inner():
        nonlocal a
#
#    return inner

variable = outer()

Step 6:
The inner function returned by the outer function is assigned to the variable and has an enclosing scope with data.

#def outer():
#    a = 4
#    
    def inner():
        nonlocal a
#
#    return inner

variable = inner
variable()  # is equal to inner()

The enclosing scope occupies less memory than the non-local scope because it contains only the dependent values. Enclosed variables/objects do not have names and are represented as memory addresses.

The closure and enclosing scope are commonly used in decorators.

Note

Every time the outer() function is called, a new inner() function is defined.

The returned inner() functions are distinct and independent objects.

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 2

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Posez-moi des questions sur ce sujet

Résumer ce chapitre

Afficher des exemples du monde réel

Awesome!

Completion rate improved to 6.67

bookImplementation of Closure

Glissez pour afficher le menu

Let's consider the example of closure:

1234567891011121314151617
def outer(argument): def inner(): nonlocal argument argument += 5 return argument return inner variable1 = outer(11) variable2 = outer(9) print("First variable1 returns", variable1()) print("Second variable1 returns", variable1()) print("First variable2 returns", variable2()) print("Second variable2 returns", variable2())
copy

We can operate with the enclosed data: the nonlocal keyword provides the ability to change argument (similar to the global keyword).

Steps to create closure

Step 1:
Create the outer function with its local scope.

def outer():
    a = 4

Step 2:
Create an inner function with local scope defined inside the outer function.

def outer():
    a = 4
    
    def inner():

Step 3:
Access the non-local (outer scope) data from within the inner local scope.

def outer():
    a = 4
    
    def inner():
        nonlocal a

Step 4:
Return the inner function and assign it to the variable.

def outer():
    a = 4
    
    def inner():
        nonlocal a

    return inner

variable = outer()

Step 5:
The local scope of the outer function is removed once it has finished executing.

#def outer():
#    a = 4
#    
    def inner():
        nonlocal a
#
#    return inner

variable = outer()

Step 6:
The inner function returned by the outer function is assigned to the variable and has an enclosing scope with data.

#def outer():
#    a = 4
#    
    def inner():
        nonlocal a
#
#    return inner

variable = inner
variable()  # is equal to inner()

The enclosing scope occupies less memory than the non-local scope because it contains only the dependent values. Enclosed variables/objects do not have names and are represented as memory addresses.

The closure and enclosing scope are commonly used in decorators.

Note

Every time the outer() function is called, a new inner() function is defined.

The returned inner() functions are distinct and independent objects.

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 2
some-alt