lambda in list comprehension acting funny
rusi
rustompmody at gmail.com
Fri Jul 13 12:46:00 EDT 2012
On Jul 13, 9:12 pm, "Prasad, Ramit" <ramit.pra... at jpmorgan.com> wrote:
> > VERBOSE = True
>
> > def function(arg):
> > if VERBOSE:
> > print("calling function with arg %r" % arg)
> > process(arg)
>
> > def caller():
> > VERBOSE = False
> > function(1)
>
> > ---------------------------------------------
> > Python semantics: function sees VERBOSE False
> > Haskell semantics: function sees VERBOSE True
> >>> def caller():
>
> ... VERBOSE = False
> ... function(1)>>> def function(arg):
>
> ... if VERBOSE:
> ... print("calling function with arg %r" % arg)
> ... >>> VERBOSE = True
> >>> caller()
>
> calling function with arg 1
>
> I might be being OCD, but caller needs `global VERBOSE` for that to
> work as you explain.
Ok let me restate: if python were to work that way (without the
global) we could say either
a Python chooses to have dynamic scoping of variables
or
b There is a bug in python's scoping rules
I would guess that most younger folks (who've not seen lisp and apl)
would choose b
We can say the same analogously in the context of ZF expressions when
the i leaks as it does in the OPs example.
Another tack on the same question: python 3 cleaned up the variable
leakage from inside ZFs to outside. It missed cleaning up the leakage
from one step to next.
tl;dr version: Beware of mixing up functional and imperative
programming
Double-beware when you are a language designer
More information about the Python-list
mailing list