Closures

Durum
Üzgünüz bu konu cevaplar için kapatılmıştır...
The sea does not like to be restrained.
Emektar Üye
Katılım
15 Tem 2021
Mesajlar
1,724
Çözümler
86
Tepki puanı
665
Ödüller
10
Yaş
25
Sosyal
4 HİZMET YILI
A closure is a function object that retains the values of variables that were present in the environment where the function was defined, even if those variables are not in scope when the function is called. In other words, a closure "closes over" the variables from its outer function, and can access and manipulate them even when the outer function has completed execution.

Closures are created in Python when a nested function references a variable from its enclosing function. Here's an example:

Python:
def outer_function(x):
    def inner_function(y):
        return x + y
    return inner_function

closure = outer_function(10)
result = closure(5)
print(result)  # Output: 15

In this example, the outer_function defines a nested function inner_function, which references the x parameter from the outer function. When outer_function is called with the argument 10, it returns the inner_function object. This object "closes over" the value of x=10, and can access and manipulate it even after outer_function has completed execution.

The resulting closure object behaves like a regular function, and can be called with a single argument (y), which is added to the "closed over" value of x. When we call closure(5), it returns 15, which is the sum of x=10 and y=5.
 
Onaylı Üye
Katılım
16 Nis 2023
Mesajlar
58
Tepki puanı
35
Ödüller
2
Yaş
5
3 HİZMET YILI
Que tipo de fechamento estamos falando ?
 
Durum
Üzgünüz bu konu cevaplar için kapatılmıştır...
Üst