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

Nick Coghlan ncoghlan at gmail.com
Tue Oct 4 09:36:11 EDT 2016


On 4 October 2016 at 23:20, Random832 <random832 at fastmail.com> wrote:
> On Tue, Oct 4, 2016, at 07:37, Nick Coghlan wrote:
>> And when you add the "else" clause that's supported by both "for" and
>> "if", what does that mean in the abbreviated form?
>>
>>     for item in items if item is not None:
>>         ...
>>     else:
>>         # ???
>>
>> Or is the implicit proposal that this form be special cased to
>> disallow the "else" clause?
>
> 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).

But would that interpretation be obvious to folks that aren't yet
aware that you can have "else" clauses on loops? (Folks can be *years*
into using Python before they first encounter that, whether in real
code or in a "Did you know <this> about Python?" snippet)

> The *real* question is what "break" should do. I think it should
> likewise break from the outermost for-loop (but "continue" should still
> continue the innermost one), but this does mean that it's not
> mechanically identical to the "equivalent" nested loops [it would,
> however, make it mechanically identical to the "generator and single
> loop" form]

Or we could stick with the status quo where limiting the keyword
chaining to the expression form naturally avoids all of these awkward
interactions with other statement level constructs.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list