lambda in list comprehension acting funny

Prasad, Ramit ramit.prasad at jpmorgan.com
Fri Jul 13 18:12:40 CEST 2012


> 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.

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  



More information about the Python-list mailing list