Deprecated: list comp leak use

T

Terry Reedy

Announcement/Warning

In the current implementation of list comprehensions, for-part control
variables leak to the surrounding context. Example:
[i for i in [1,2]] [1, 2]
print i
2
The name i was bound within the list comprehension. The Reference
Manual currently says nothing about this behavior.

On py-dev today (now yesterday), after GvR described how the leak
could be plugged, and other developers urged that it be so plugged,
GvR declared use of such leaked bindings as deprecated, and the
leakage subject to cessation in a future version .

Since people have posted at least one clever hack exploiting the
leakage (for simulating assignment expressions), I though this
followup might be useful.

This decision and any future patches will *NOT* affect for-loop
variables. The following search idiom, for instance, will continue to
work:

for item in seq:
if pred(item): break
else:
item = default #usually sentinal value such as None
# item is now first item meeting pred criterion or default

Terry J. Reedy
 
J

John Roth

Terry Reedy said:
Announcement/Warning

In the current implementation of list comprehensions, for-part control
variables leak to the surrounding context. Example:
[i for i in [1,2]] [1, 2]
print i
2
The name i was bound within the list comprehension. The Reference
Manual currently says nothing about this behavior.

However, the tutorial for 2.2.3 not only describes it, but it
gives an example of how it can be used.

John Roth
 
T

Terry Reedy

John Roth said:
However, the tutorial for 2.2.3 not only describes it,

That I see
but it gives an example of how it can be used.

That I don't, unless you mean this4

Thanks. I will point this out to appropriate persons.

Terry J. Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top