Lambdas in Python does not play by the same rules as anonymous functions in MATLAB
- MATLAB takes a snapshot of (capture) the workspace variables involved in the anonymous function AT the time the anonymous function handle is created, thus the captured values will live on afterwards (by definition a proper closure).
- Lambda is Python is NOT closure! The local variables involved in the expression are simply read on-the-fly (aka, the last state) when the functor is executed.
It’s kind of a mixed love-and-hate situation for both. Either design choice will be confusing for some use cases. I was at first thrown off by MATLAB’s closure, then after I get used to it (how else are you going to curry?), Python’s Lambda’s non-closure tripped me. Even in the official FAQ, it address the surprise that people are not getting what they expected creating lambdas in a for-loop.
To enable capture in Python, you assign the value you wanted to capture to a lambda argument (intermediary), then use the intermediary in the expression.
lambda: ser.close() # does not capture 'ser' lambda s=ser: s.close() # 'ser' is captured by s. NOTE: still not closure!
I usually keep the usage of nested functions to the minimum, even in MATLAB, because effectively it’s kind of a compromised ‘global’ between nested levels, or a little bit like protected classes in C++. It breaks encapsulation (intentionally) for functions in your inner circle (nest).
It’s often useful for coding up GUI in MATLAB quick because you need to share access to the UI controls within the same group. For GUI that gets more complicated, I actually avoided nested functions altogether and used *appdata() to share UI object handles.
Functors of nested functions are closures in both MATLAB and Python! Only Lambdas in Python behave slightly differently.
86 total views, 1 views today