4 ms·
Just been reading up on this. Python has sort of read only closures where the free variables are stored against the functions. You can't alter a variable inside
by aidos 11y ago
Just been reading up on this. Python has sort of read only closures where the free variables are stored against the functions. You can't alter a variable inside the closure like you can in JS. Personally, I don't mind - I'd rather have a better idea of which scopes are changing which information, but I don't think they're considered "true closures."
More over here - http://stackoverflow.com/a/23558809/171450 http://stackoverflow.com/a/23558809/171450
- gte525u 11y agoThis is incorrect for at least python 3.x I.e., Python 3.4.3 (default, Apr 7 2015, 08:05:21) >>> def mk_generator(initial): ... var = initial ... def increment(count=1): ... nonlocal var ... var += count ... return var ... return increment ... >>> a = mk_generator(3) >>> b = mk_generator(10) >>> a(1) 4 >>> b <function mk_generator.<locals>.increment at 0x1040a7d08> >>> b() 11 >>> b() 12 >>> a(3) 7