closure
-
DecoratorsDynamicPL/Python 2019. 11. 7. 18:36
1. Overview In general a decorator function: Takes a function as an argument Returns a closure The closure usually accepts any combination of parameters Runs some code in the inner function (closure) The closure function calls the original function using the arguments passed to the closure Returns whatever is returned by that function call 2. Description 2.1 Decorator function def counter(fn): c..
-
ClosureDynamicPL/Python 2019. 11. 7. 17:09
1. Overview Closure can be consist of a function plus an extended scope that contains the free variables. 2. Description 2.1 Free Variable and Inner Function Functions defined inside another function can access the outer (nonlocal) variables def outer(): x = [1, 2, 3] print('outer:', hex(id(x))) def inner(): print('inner:', hex(id(x))) print(x) return inner fn = outer() fn() x is a free variable..