[Python-ideas] BUG in standard while statement
MRAB
python at mrabarnett.plus.com
Fri Sep 11 00:12:39 CEST 2015
On 2015-09-10 20:04, Chris Barker wrote:
> 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.
>
You could use a generator expression with a function that discards the
results:
def every(iterable):
for _ in iterable:
pass
every(obj.a_method() for obj in a_sequence)
>
> On Thu, Sep 10, 2015 at 12:07 AM, Terry Reedy <tjreedy at udel.edu
> <mailto: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.
>
More information about the Python-ideas
mailing list