Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Odd behaviour with list comprehension

Reply
Thread Tools

Re: Odd behaviour with list comprehension

 
 
Jerry Hill
Guest
Posts: n/a
 
      03-01-2008
On Fri, Feb 29, 2008 at 10:01 PM, Ken Pu <(E-Mail Removed)> wrote:
> Is there a way for me keep the iterating variable in list
> comprehension local to the list comprehension?


Kind of. You can use a generator expression instead of a list
comprehension, and those don't leak their internal variables into the
enclosing scope:

>>> list(x for x in range(10))

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> x


Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
x
NameError: name 'x' is not defined
>>>


You have to pass it to the list constructor to get a list out of it,
though. For more on generator expressions see PEP 289:
http://www.python.org/dev/peps/pep-0289/

--
Jerry
 
Reply With Quote
 
 
 
 
Micah Cowan
Guest
Posts: n/a
 
      03-01-2008
"Jerry Hill" <(E-Mail Removed)> writes:

> On Fri, Feb 29, 2008 at 10:01 PM, Ken Pu <(E-Mail Removed)> wrote:
>> Is there a way for me keep the iterating variable in list
>> comprehension local to the list comprehension?

>
> Kind of. You can use a generator expression instead of a list
> comprehension, and those don't leak their internal variables into the
> enclosing scope:


Whoa, that's cool. I didn't even think to try that, just assuming it
would do the same.

Though now that I think about it, I don't see how it possibly could,
since it just evaluates to an object that you could pass around, and
return, using it the same way elsewhere.

--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/
 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      03-31-2008
Micah Cowan wrote:
> "Jerry Hill" <(E-Mail Removed)> writes:
>
>> On Fri, Feb 29, 2008 at 10:01 PM, Ken Pu <(E-Mail Removed)> wrote:
>>> Is there a way for me keep the iterating variable in list
>>> comprehension local to the list comprehension?

>> Kind of. You can use a generator expression instead of a list
>> comprehension, and those don't leak their internal variables into the
>> enclosing scope:

>
> Whoa, that's cool. I didn't even think to try that, just assuming it
> would do the same.
>
> Though now that I think about it, I don't see how it possibly could,
> since it just evaluates to an object that you could pass around, and
> return, using it the same way elsewhere.
>

Well, the fact that the bound variable in the list comprehension does
indeed remain outside the construct is simply the result of an
over-zealous wish to emulate for loop semantics. The original reasoning,
IIRC, as that since a for loop left its bound variable at the last used
value, so should a list comprehension.

This was quickly recognized as a mistake, but unfortunately not quickly
enough. As it was felt that some people might already have relied on
that behavior, it was retained in the interests of preserving backwards
compatibility.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      03-31-2008

"Steve Holden" <(E-Mail Removed)> wrote in message
news:fspe8a$ptq$(E-Mail Removed)...
| Micah Cowan wrote:
| > "Jerry Hill" <(E-Mail Removed)> writes:
| >
| >> On Fri, Feb 29, 2008 at 10:01 PM, Ken Pu <(E-Mail Removed)>
wrote:
| >>> Is there a way for me keep the iterating variable in list
| >>> comprehension local to the list comprehension?
| >> Kind of. You can use a generator expression instead of a list
| >> comprehension, and those don't leak their internal variables into the
| >> enclosing scope:
| >
| > Whoa, that's cool. I didn't even think to try that, just assuming it
| > would do the same.
| >
| > Though now that I think about it, I don't see how it possibly could,
| > since it just evaluates to an object that you could pass around, and
| > return, using it the same way elsewhere.
| >
| Well, the fact that the bound variable in the list comprehension does
| indeed remain outside the construct is simply the result of an
| over-zealous wish to emulate for loop semantics. The original reasoning,
| IIRC, as that since a for loop left its bound variable at the last used
| value, so should a list comprehension.
|
| This was quickly recognized as a mistake, but unfortunately not quickly
| enough. As it was felt that some people might already have relied on
| that behavior, it was retained in the interests of preserving backwards
| compatibility.

But it will not be retained in 3.0, as I understand it.
So best to not exploit the deprecated behavior.

tjr



 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      03-31-2008
Terry Reedy wrote:
> "Steve Holden" <(E-Mail Removed)> wrote in message

[...]
> | Well, the fact that the bound variable in the list comprehension does
> | indeed remain outside the construct is simply the result of an
> | over-zealous wish to emulate for loop semantics. The original reasoning,
> | IIRC, as that since a for loop left its bound variable at the last used
> | value, so should a list comprehension.
> |
> | This was quickly recognized as a mistake, but unfortunately not quickly
> | enough. As it was felt that some people might already have relied on
> | that behavior, it was retained in the interests of preserving backwards
> | compatibility.
>
> But it will not be retained in 3.0, as I understand it.
> So best to not exploit the deprecated behavior.
>

Correct. The compatibility break provided by 3.0 allows the developers
to drop this historical grunge.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List comprehension + lambdas - strange behaviour Artur Siekielski Python 6 05-07-2010 08:54 PM
List comprehension in if clause of another list comprehension Vedran Furac( Python 4 12-19-2008 01:35 PM
Odd behaviour with list comprehension Ken Pu Python 2 03-01-2008 04:58 AM
Appending a list's elements to another list using a list comprehension Debajit Adhikary Python 17 10-18-2007 06:45 PM
List comprehension returning subclassed list type? Shane Geiger Python 4 03-25-2007 09:34 AM



Advertisments