Path: csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!feeder.news-service.com!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.01; '2.1': 0.04; 'wed,': 0.04; '(although': 0.05; 'arguments': 0.05; 'sure.': 0.05; 'function,': 0.07; 'pep': 0.07; 'python': 0.07; '__future__': 0.09; 'googling': 0.09; 'namespace': 0.09; 'subject:()': 0.09; 'thinking,': 0.09; 'url:dev': 0.09; 'url:peps': 0.09; 'pm,': 0.11; 'wrote:': 0.14; "computer's": 0.16; "function's": 0.16; 'infinitely': 0.16; 'lambda': 0.16; 'namespace,': 0.16; 'namespace.': 0.16; 'odd,': 0.16; 'okay,': 0.16; 'programmer,': 0.16; 'recursive': 0.16; 'scopes': 0.16; 'then?': 0.16; 'tend': 0.16; 'code.': 0.18; "'python": 0.19; 'describes': 0.19; 'subject:List': 0.20; 'language': 0.20; 'programming': 0.20; 'seems': 0.21; '(like': 0.22; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'received:209.85.214.174': 0.23; 'received:mail-iw0-f174.google.com': 0.23; 'example': 0.24; 'mainly': 0.24; 'mine': 0.25; 'guess': 0.26; 'chris': 0.27; 'changed': 0.27; 'tried': 0.27; 'function': 0.27; 'message- id:@mail.gmail.com': 0.28; "doesn't": 0.28; 'do.': 0.31; 'boss,': 0.31; 'context,': 0.31; 'nested': 0.31; 'outer': 0.31; 'parent': 0.31; 'turns': 0.31; 'import': 0.32; 'to:addr:python-list': 0.32; 'change': 0.34; 'there': 0.35; 'put': 0.35; 'languages.': 0.35; 'saves': 0.35; 'usually': 0.36; 'too': 0.36; 'think': 0.36; 'some': 0.37; 'received:209.85': 0.37; 'url:python': 0.37; 'either': 0.37; '20,': 0.38; 'apr': 0.38; 'involving': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'languages': 0.38; 'url:org': 0.38; 'anything': 0.38; 'tim': 0.39; 'received:209.85.214': 0.39; 'though,': 0.39; 'comments': 0.39; 'common': 0.39; 'explain': 0.39; 'to:addr:python.org': 0.39; 'where': 0.39; 'received:209': 0.39; 'how': 0.39; 'except': 0.39; "it's": 0.40; 'header:Received:5': 0.40; 'exact': 0.60; '2011': 0.62; 'body': 0.62; 'became': 0.65; 'minutes': 0.67; 'details,': 0.68; 'fifteen': 0.84; 'roberts': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=/WD6afbcgJzGHtsBX+lnKUcE8RV+Cbn1A81tRwVjh6c=; b=YH6v2kCBjlYbG0wssm8EYAo5MAnTELhH9/7AijxnfcwmiuAPOowMojlYhaKFrNgd4+ 3pPHeS6w1FnxSFOSSldyVdEW/Gd5LmRoDrypymjeaJyPeA51G/a0Hsh4aCGVl5bi9saj 8BHh+wwMe1J+Sr5ZGd3hiBVSjQFMvVdG/26ws= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=FrV0RChqbfthHwlsekN8dWVMymOEpZAmaFY/WRsDX1+NDDPJW19LiTkOszcWaM/j5r leogtNKLP+CxhFMSp/X2HdR69AT08gekiuqGgG2qzVAgsoVY1Mj4sJsZLotdaA40s8Kq NmPb5GePwKawdQupKL5Z91PDS2emdKEHcfMTs= MIME-Version: 1.0 In-Reply-To: References: Date: Wed, 20 Apr 2011 18:36:08 +1000 Subject: Re: List comprehension vs filter() From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 40 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1303288571 news.xs4all.nl 81481 [::ffff:82.94.164.166]:37063 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3669 On Wed, Apr 20, 2011 at 5:16 PM, Tim Roberts wrote: > It's because, unlike some other languages (like Pascal), Python doesn't > have infinitely recursive nested namespaces. =A0Glossing over details, th= ere > is a global namespace, and there is a local namespace. =A0A new function = gets > a new local namespace. =A0"posttype" is part of the local namespace of th= e > outer function, but it's not part of the local namespace of the lambda. Okay, well that saves me from going insane anyhow! (Although it may be too late to save me from that.) I skimmed your email and then tried to explain it all to my boss, who has some programming experience but only rudimentary Python, and after ten or fifteen minutes we concluded that I needed to put some comments in the code. > You can solve this through the common lamba idiom of a closure: > > lst=3Dfilter(lambda x,posttype=3Dposttype: x["type"].lower()=3D=3Dposttyp= e,lst) Seems a little odd, but sure. I guess this means that a function's default arguments are evaluated in the parent context, but the body is evaluated in its own context? >From what I understand, though, from __future__ import nested_scopes became a permanent part of the language long before 2.6.6, and PEP 227 describes this exact situation of lambda functions: http://www.python.org/dev/peps/pep-0227/ gives an example involving Tkinter. Unfortunately googling for 'python nested scope' mainly turns up old information from 2.1 and thereabouts; has anything changed since then? Infinitely-nested scopes is definitely how I, as a programmer, tend to think about my code. Any place where the computer's interpretation of the code differs from mine is a place where either the computer needs to change its thinking, or I do. And it's usually easier to change me, except that I use so many languages. :) Chris Angelico