|
|
|
|
|
by julian37
5101 days ago
|
|
If you look closely, foo is an anonymous function No, "foo" is a variable holding a reference to a named function--the function named "closure". You seem to be confused as to what an anonymous function is. (It doesn't per se have anything to do with closures.) GP is right, Python's lambdas are crippled, unfortunately. And I'm saying that as someone who loves Python. |
|
Call the outer function 3 times. You'll wind up with 3 functions. They will be independent, even though you think they are all named the same thing.
There is literally nothing you could want to do with anonymous functions that you cannot do with this technique. (Of course if you want to update an enclosed variable, you have to store it in a mutable data structure. But that is true for any Python function that wants to mutate data in its surrounding environment.)