[Python-ideas] if-statement in for-loop

Chris Angelico rosuav at gmail.com
Tue Oct 4 11:49:02 EDT 2016


On Wed, Oct 5, 2016 at 2:42 AM, David Mertz <mertz at gnosis.cx> wrote:
> On Oct 4, 2016 6:20 AM, "Random832" <random832 at fastmail.com> wrote:
>> >     for item in items if item is not None:
>> >         ...
>> >     else:
>> >         # ???
>
>>
>> I think it's obvious that it would be on the outermost construct (i.e.
>> the one that would still be at the same indentation level fully
>> expanded).
>
> I think it's obvious it would be the innermost construct... Or at least very
> plausible.

My reading of this is that the loop consists of a single filtered
iteration, ergo break/continue/else are as if the loop used a
generator:

# for item in items if item is not None:
for item in (item for item in items if item is not None):

These two would be semantically equivalent, and the first one has the
advantage of not sounding like the Cheshire Cat as Alice entered
'Machinations'.

<< Time to jump in time to jump through time.... I'm dizzy. >>

ChrisA


More information about the Python-ideas mailing list