[Python-ideas] Control Flow - Never Executed Loop Body

Michel Desmoulin desmoulinmichel at gmail.com
Wed Apr 6 10:49:05 EDT 2016


What about:

>>> for item in my_iterable as iterator:
>>>     # do per item
>>> if iterator.empty: # empty
>>>    # this code gets executed if iterator was empty
>>> else: # not empty

The empty marker would be set to True but the for loop and set to False
when iterating on the loop. The perf cost of this is only added if you
explicitly requires it with the "as" keyword.

You don't handle break, which is handle by else anyway.

And maybe having access to the iterator could open the door to more
features.

Le 06/04/2016 16:16, Thomas Güttler a écrit :
> 
> 
> Am 24.03.2016 um 18:44 schrieb Sven R. Kunze:
>> On 24.03.2016 14:54, Thomas Güttler wrote:
>>> for item in my_iterator:
>>>     # do per item
>>> on empty:
>>>    # this code gets executed if iterator was empty
>>> on break:
>>>    # this code gets executed if the iteration was left by a "break"
>>> on notempty:
>>>    # ...
>>>
>>
>> Hmm, interesting. "on" would indeed be a distinguishing keyword to
>> "except". So, "except" handles exceptions and "on"
>> handles internal control flow (the iter protocol). Nice idea actually.
> 
> I thank you, because you had the idea to extend the loop syntax :-)
> 
> But I guess it is impossible to get "on empty" implemented.
> 
> 
> Regards,
>   Thomas Güttler
> 


More information about the Python-ideas mailing list