Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'interpreter': 0.04; 'caller': 0.07; 'completeness': 0.07; 'subject:skip:c 10': 0.07; 'python': 0.09; '%r"': 0.09; 'arg': 0.09; 'defined.': 0.09; 'does,': 0.09; 'received:155': 0.09; 'def': 0.10; 'stack': 0.15; '"python': 0.16; '`function`': 0.16; 'binding.': 0.16; 'disclaimers': 0.16; 'disclaimers,': 0.16; 'from:addr:jpmorgan.com': 0.16; 'lexical': 0.16; 'local.': 0.16; 'received:155.180': 0.16; 'received:159.53': 0.16; 'received:159.53.110': 0.16; 'received:159.53.110.174': 0.16; 'received:bankone.net': 0.16; 'received:exchad.jpmchase.net': 0.16; 'received:jpmchase.com': 0.16; 'received:jpmchase.net': 0.16; 'received:sj2.jpmchase.com': 0.16; 'received:svr.bankone.net': 0.16; 'securities,': 0.16; 'sees': 0.16; 'url:disclosures': 0.16; 'url:jpmorgan': 0.16; 'variable.': 0.16; 'verbose': 0.16; 'writes.': 0.16; 'refers': 0.17; 'variables': 0.17; 'variable': 0.20; 'to:name:python- list@python.org': 0.20; 'skip:- 40': 0.21; 'assignment': 0.22; 'programming': 0.23; 'received:169.254': 0.24; 'header:In-Reply- To:1': 0.25; 'question': 0.27; 'accuracy': 0.27; 'subject:list': 0.28; '>>>>': 0.29; 'case,': 0.29; 'received:169': 0.29; 'function': 0.30; 'point': 0.31; 'to:addr:python-list': 0.33; 'languages': 0.33; 'text': 0.34; 'false': 0.35; 'there': 0.35; 'add': 0.36; 'but': 0.36; 'be.': 0.36; 'depends': 0.36; 'charset :us-ascii': 0.36; 'two': 0.37; 'being': 0.37; 'quite': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'where': 0.40; 'your': 0.60; 'most': 0.61; 'remove': 0.61; 'different': 0.63; 'information,': 0.63; 'url:email': 0.63; 'legal': 0.65; 'subject': 0.66; 'purchase': 0.67; 'sale': 0.76; 'received:169.254.8': 0.84; 'mean.': 0.91; 'rusi': 0.91; 'subject:funny': 0.91 X-DKIM: OpenDKIM Filter v2.1.3 sj2.jpmchase.com q6DI6ffU018194 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jpmorgan.com; s=smtpout; t=1342202801; bh=12aHH8hGoSGKOlpUwOxp7gN6f0iQ0ijnlBpeAQlErf8=; h=From:To:Subject:Date:Message-ID:References:In-Reply-To: Content-Transfer-Encoding:MIME-Version:Content-Type; b=ZGfOwEdNECFsZUWr+pI9xw7S1lH3T02w7a6qHTVZf37mVN8+ldOR6R2bcqK72jmqq dmkhrTU0lZYcFoDEbD/UNpZ7dWDVPJKlf8/8KtQ9CRQKXUxBeQcE9i0kU5n1ynUd7a Vl5OX7+6sSTk0jvCzJaZNp7xqcJAb8i6FMyhFlE8= From: "Prasad, Ramit" To: "python-list@python.org" Subject: RE: lambda in list comprehension acting funny Thread-Topic: lambda in list comprehension acting funny Thread-Index: AQHNYSBhMI5tpoL+PkusODrgV6TbgZcngObg Date: Fri, 13 Jul 2012 18:06:31 +0000 References: <03943ea6-5914-4de4-821c-055bed1d5804@f9g2000pbd.googlegroups.com> <4fffc1fd$0$29965$c3e8da3$5496439d@news.astraweb.com> <5000608c$0$6930$e4fe514c@news2.news.xs4all.nl> In-Reply-To: <5000608c$0$6930$e4fe514c@news2.news.xs4all.nl> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.67.79.47] Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-DLP-FWD: Yes Content-Type: text/plain; charset="us-ascii" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1342202805 news.xs4all.nl 6949 [2001:888:2000:d::a6]:49661 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25281 > >> VERBOSE =3D True=0D=0A> >>=0D=0A> >> def function(arg):=0D=0A> >> = if VERBOSE:=0D=0A> >> print("calling function with arg %r" % arg)=0D= =0A> >> process(arg)=0D=0A> >>=0D=0A> >> def caller():=0D=0A> >> VE= RBOSE =3D False=0D=0A> >> function(1)=0D=0A> >>=0D=0A> >> -------------= --------------------------------=0D=0A> >> Python semantics: function sees = VERBOSE False=0D=0A> >> Haskell semantics: function sees VERBOSE True=0D=0A= > =0D=0A> >>>> def caller():=0D=0A> > =2E=2E=2E VERBOSE =3D False=0D=0A= > > =2E=2E=2E function(1)=0D=0A> >>>> def function(arg):=0D=0A> > =2E= =2E=2E if VERBOSE:=0D=0A> > =2E=2E=2E print("calling function wi= th arg %r" % arg)=0D=0A> > =2E=2E=2E=0D=0A> >>>> VERBOSE =3D True=0D=0A> >>= >> caller()=0D=0A> > calling function with arg 1=0D=0A> >=0D=0A> > I might = be being OCD, but caller needs `global VERBOSE` for that to=0D=0A> > work a= s you explain=2E=0D=0A> =0D=0A> That would be quite different from what Rus= i is after=2E=0D=0A> =0D=0A> If you add `global VERBOSE` to `caller`, then = there is only one=0D=0A> variable named `VERBOSE` and what `function` does,= depends on=0D=0A> the most recent assignment to that variable=2E=0D=0A> = =0D=0A> If you remove your `global VERBOSE`, then there are two=0D=0A> vari= ables by that name, one global and one local to `caller`=2E=0D=0A> In that = case, there is the question of which one `function`=0D=0A> will use=2E=0D= =0A>=0D=0A=0D=0ABut that is not what Rusi writes=2E=0D=0A"Python semantics:= function sees VERBOSE False" <- function=0D=0Awill not see False without t= he use of global=2E =0D=0A=0D=0A> The function `function` refers to a varia= ble `VERBOSE` that=0D=0A> isn't local=2E In some programming langauages, t= he interpreter=0D=0A> would then scan the call stack at run-time, looking f= or a scope=0D=0A> where that name is defined=2E It would find the local on= e in=0D=0A> `caller`=2E This is known as "dynamic binding"=2E=0D=0A> =0D= =0A> Other interpreters use the `VERBOSE` that was in scope at=0D=0A> the p= oint in the program text where `function` was defined=2E=0D=0A> In this cas= e, that would be the global one=2E This is called=0D=0A> "lexical binding"= =2E=0D=0A> =0D=0A> Some programming languages allow you to indicate on a pe= r-=0D=0A> variable basis whether you want dynamic or lexical binding=2E=0D= =0A> =0D=0A> Python is firmly in the lexical camp=2E Dynamic binding is no= t=0D=0A> available in Python, and never will be=2E=0D=0A=0D=0ATrue and a go= od explanation, but not what I understood=0D=0ARusi to mean=2E=0D=0A=0D=0AR= amit=0D=0A=0D=0A=0D=0A=0D=0AThis email is confidential and subject to impor= tant disclaimers and=0D=0Aconditions including on offers for the purchase o= r sale of=0D=0Asecurities, accuracy and completeness of information, viruse= s,=0D=0Aconfidentiality, legal privilege, and legal entity disclaimers,=0D= =0Aavailable at http://www=2Ejpmorgan=2Ecom/pages/disclosures/email=2E