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


Groups > comp.lang.python > #3714

Re: List comprehension vs filter()

References <mailman.603.1303269025.9059.python-list@python.org> <iomd86$f4r$1@solani.org>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2011-04-20 08:44 -0600
Subject Re: List comprehension vs filter()
Newsgroups comp.lang.python
Message-ID <mailman.648.1303310730.9059.python-list@python.org> (permalink)

Show all headers | 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