Why aren't Python nested functions called closures?
Why aren't Python nested functions called closures?
Nested functions in Python are functions defined within other functions. They are not necessarily closures, although they can lead to the creation of closures. A closure, in contrast, is a specific type of nested function that captures and retains access to variables from its enclosing scope, even after the outer function has finished executing.
The distinction between nested functions and closures lies in the concept of "free variables." A free variable is a variable that is not bound in the local scope but is accessed by a nested function. When a nested function captures these free variables, it becomes a closure. This means that the nested function retains access to those variables even when the function in which it was defined has completed execution.
Nested functions become closures when they satisfy the following conditions:
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.