lambda in list comprehension acting funny
ian.g.kelly at gmail.com
Fri Jul 13 20:54:02 CEST 2012
On Fri, Jul 13, 2012 at 11:53 AM, Hans Mulder <hansmu at xs4all.nl> wrote:
> The function `function` refers to a variable `VERBOSE` that
> isn't local. In some programming langauages, the interpreter
> would then scan the call stack at run-time, looking for a scope
> where that name is defined. It would find the local one in
> `caller`. This is known as "dynamic binding".
> Other interpreters use the `VERBOSE` that was in scope at
> the point in the program text where `function` was defined.
> In this case, that would be the global one. This is called
> "lexical binding".
> Some programming languages allow you to indicate on a per-
> variable basis whether you want dynamic or lexical binding.
> Python is firmly in the lexical camp. Dynamic binding is not
> available in Python, and never will be.
I don't believe that dynamic vs. lexical binding is what rusi was
attempting to describe. If he was, then Python and Haskell would be a
bad comparison since both are lexical. Rather, I think what he was
trying to show was capture by reference vs. capture by value in the
context of closures. Python uses capture by reference, and so the
upvalue is the value of that reference at the time the closure is
called. Haskell uses capture by value, and the upvalue is the value
at the time of definition.
I've also seen the distinction described as "early" vs. "late" binding
on this list, but I'm not sure how precise that is -- I believe that
terminology more accurately describes whether method and attribute
names are looked up at compile-time or at run-time, late binding being
the feature that makes duck typing possible.
More information about the Python-list