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


Groups > comp.lang.python > #3714

Re: List comprehension vs filter()

Return-Path <ian.g.kelly@gmail.com>
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; 'wed,': 0.04; 'exec': 0.07; 'interpreter': 0.07; 'type,': 0.07; 'python': 0.07; '>>>>': 0.09; 'subject:()': 0.09; '>>>': 0.12; 'def': 0.13; 'am,': 0.14; 'wrote:': 0.14; '"copyright",': 0.16; '"credits"': 0.16; '"license"': 0.16; '[gcc': 0.16; 'f()': 0.16; 'lambda': 0.16; 'linux2': 0.16; 'namespace,': 0.16; 'namespace;': 0.16; 'op:': 0.16; 'fine': 0.18; 'functions,': 0.19; 'subject:List': 0.20; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.20; 'cheers,': 0.20; 'header:In-Reply-To:1': 0.22; 'cc:addr:python-list': 0.22; 'function': 0.27; 'message-id:@mail.gmail.com': 0.28; 'cc:addr:python.org': 0.31; 'received:209.85.210.46': 0.31; 'received:mail-pz0-f46.google.com': 0.31; 'using': 0.34; 'file': 0.35; 'print': 0.35; 'question': 0.35; 'assignment': 0.35; 'two': 0.37; 'received:209.85': 0.37; 'either': 0.37; 'run': 0.37; '20,': 0.38; 'apr': 0.38; 'received:google.com': 0.38; 'so,': 0.38; 'received:209': 0.39; 'works': 0.40; 'header:Received:5': 0.40; '2011': 0.62; 'reads': 0.65; '2.7.1': 0.84; 'same:': 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:from:date :message-id:subject:to:cc:content-type; bh=zAQjbLz2ST0Oy/HPkf/4JnBCTDslH414CjkWnXHbW/A=; b=wSP3leqaccmzBgJktRdO65FI+V0pca7gN7QbAUVLk5zvK7JAffqnxOKPPSXk9RfdSr sezYyl+KQ+uZZ/cXNigpiXBD5RVplGhdxNefh+uoTsL8TLCDWaxOsRV7vppPblrrjlui 4z0Le8BVEMyEJdR4LXD4MKoMd/1r5qOLVFL7Y=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; b=tWNJcq4SI7nuQOjB8SgFwKOA9aRWM5G281vMWBPc5NU4kKg7N9uDR7YK8HXqYCptr4 kSsgBYdgMth/ekkLih8IsguU2zXS0v0Y9xVL/BrqyxKUjUzdWzVIv8uKLk+CFpvnjdoF W/iY5+JA526oNZdlrqHgMvnrPmcRxTtNu1MkA=
MIME-Version 1.0
In-Reply-To <iomd86$f4r$1@solani.org>
References <mailman.603.1303269025.9059.python-list@python.org> <iomd86$f4r$1@solani.org>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Wed, 20 Apr 2011 08:44:58 -0600
Subject Re: List comprehension vs filter()
To Peter Otten <__peter__@web.de>
Content-Type text/plain; charset=ISO-8859-1
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.648.1303310730.9059.python-list@python.org> (permalink)
Lines 32
NNTP-Posting-Host 82.94.164.166
X-Trace 1303310731 news.xs4all.nl 41113 [::ffff:82.94.164.166]:40382
X-Complaints-To abuse@xs4all.nl
Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.stben.net!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!feeder2.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.84.MISMATCH!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:3714

Show key headers only | View raw


On Wed, Apr 20, 2011 at 4:41 AM, Peter Otten <__peter__@web.de> wrote:
> The assignment writes to the local namespace, the lambda function reads from
> the global namespace; this will only work as expected if the two namespaces
> are the same:
>
>>>> exec """type = 42; print filter(lambda x: x == type, [42])""" in {}, {}
> []
>>>> ns = {}
>>>> exec """type = 42; print filter(lambda x: x == type, [42])""" in ns
> [42]

That must be a quirk of exec, because it works just fine without using
exec, both in and out of functions, either from the interactive
interpreter or from a script:

Python 2.7.1 (r271:86832, Apr  2 2011, 19:44:19)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
...     t = 42
...     print filter(lambda x: x == t, [42])
...
>>> f()
[42]
>>> t = 43
>>> print filter(lambda x: x == t, [43])
[43]

So, the question for the OP:  Is this file being run with execfile?

Cheers,
Ian

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


Thread

List comprehension vs filter() Chris Angelico <rosuav@gmail.com> - 2011-04-20 13:10 +1000
  Re: List comprehension vs filter() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-04-20 10:16 +0000
    Re: List comprehension vs filter() Chris Angelico <rosuav@gmail.com> - 2011-04-20 21:25 +1000
  Re: List comprehension vs filter() Peter Otten <__peter__@web.de> - 2011-04-20 12:41 +0200
    Re: List comprehension vs filter() Ian Kelly <ian.g.kelly@gmail.com> - 2011-04-20 08:44 -0600
    Re: List comprehension vs filter() Chris Angelico <rosuav@gmail.com> - 2011-04-21 04:03 +1000
    Re: List comprehension vs filter() Ian Kelly <ian.g.kelly@gmail.com> - 2011-04-20 12:35 -0600

csiph-web