Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #3661

Re: List comprehension vs filter()

From Tim Roberts <timr@probo.com>
Newsgroups comp.lang.python
Subject Re: List comprehension vs filter()
Date 2011-04-20 00:16 -0700
Organization Providenza & Boekelheide, Inc.
Message-ID <lt1tq6hq1oo39n0tl87evb1vi8cj841vqu@4ax.com> (permalink)
References <BANLkTin4KMcoWzfH64Eu=+sD9=M3TaAu2A@mail.gmail.com> <BANLkTinVhX1jDd5brOh1D1m4zU5R8pEoSQ@mail.gmail.com> <BANLkTimvUPXRKOvGNqDzNCJp0UuWT1AkVg@mail.gmail.com> <BANLkTimmfwZ5dB+Vt8S2-rYAwS4axvn8og@mail.gmail.com> <mailman.611.1303271986.9059.python-list@python.org>

Show all headers | View raw


Chris Angelico <rosuav@gmail.com> wrote:

>On Wed, Apr 20, 2011 at 1:45 PM, Chris Rebert <clp2@rebertia.com> wrote:
>> Built-ins aren't quite the same as globals, but essentially yes:
>
>Sure. That might explain some of the weirdness, but it doesn't explain
>why things were still weird with the variable named posttype. 

It's because, unlike some other languages (like Pascal), Python doesn't
have infinitely recursive nested namespaces.  Glossing over details, there
is a global namespace, and there is a local namespace.  A new function gets
a new local namespace.  "posttype" is part of the local namespace of the
outer function, but it's not part of the local namespace of the lambda.

You can solve this through the common lamba idiom of a closure:

lst=filter(lambda x,posttype=posttype: x["type"].lower()==posttype,lst)
-- 
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Re: List comprehension vs filter() Chris Angelico <rosuav@gmail.com> - 2011-04-20 13:59 +1000
  Re: List comprehension vs filter() Tim Roberts <timr@probo.com> - 2011-04-20 00:16 -0700
    Re: List comprehension vs filter() Chris Angelico <rosuav@gmail.com> - 2011-04-20 18:36 +1000
      Re: List comprehension vs filter() Mel <mwilson@the-wire.com> - 2011-04-20 10:12 -0400

csiph-web