[Python-ideas] BUG in standard while statement

Chris Barker chris.barker at noaa.gov
Thu Sep 10 21:04:23 CEST 2015


however, he did bring up a python-idea worthy general topic:

Sometimes you want to iterate without doing anything with the results of
the iteration.

So the obvious is his example -- iterate N times:

for i in range(N):
    do_something

but you may need to look into the code (probably more than one line) to see
if i is used for anything.

I know there was talk way back about making integers iterable, so you could
do:

for i in 32:
   do something.

which would be slightly cleaner, but still has an extra i in there, and
this was soundly rejected anyway (for good  reason). IN fact, Python's
"for" is not really about iterating N times, it's about iteraton over a
sequence of objects. Ans I for one find:

for _ in range(N):

To be just fine -- really very little noise or performance overhead or
anything else.

However, I've found myself wanting a "make nothing comprehension". For some
reason, I find myself frequently following a pattern where I want to call
the same method on all the objects in a sequence:

for obj in a_sequence:
    obj.a_method()

but I like the compactness of comprehensions, so I do:

[obj.a_method() for obj in a_sequence]

but then this creates a list of the result from that method call. Which I
don't want, so I don't assign the results to anything, and it just goes
away.

But somehow it bugs me that I'm creating this (maybe big) ol' list full of
junk, just to have it deleted.

Anyone else think this is a use-case worth supporting better? Or should I
jstu get over it -- it's really not that expensive to create a list, after
all.

-Chris








On Thu, Sep 10, 2015 at 12:07 AM, Terry Reedy <tjreedy at udel.edu> wrote:

> On 9/9/2015 1:10 PM, Stephan Sahm wrote:
>
> I found a BUG in the standard while statement, which appears both in
>> python 2.7 and python 3.4 on my system.
>>
>
> No you did not, but aside from that: python-ideas is for ideas about
> future versions of python, not for bug reports, valid or otherwise.  You
> should have sent this to python-list, which is a place to report possible
> bugs.
>
> --
> Terry Jan Reedy
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150910/0cf9fd58/attachment-0001.html>


More information about the Python-ideas mailing list