In all my programming career as a python programmer I have never used a
for-else construct. In fact, I won't be able to tell you exactly when the
else-block is executed off the top of my head either. Even though that
probably speaks more about me as a programmer than about the python
language, it also underlines the fact that "else" is likely the most
misplaced term here.

For what it's worth, django template language adopts an "empty" keyword for
thes block that will execute if the for block didn't (if the iterator was
empty). For-empty.

Is there, however, no way to check if the iterator was exhausted or not?
Wouldn't this be nice to have:

for x in iter {
...
}

if (iter.exhausted()) {
...
}
 On Aug 10, 2013 6:36 PM, "Simon Sapin" <simon.sa...@exyr.org> wrote:

> Le 10/08/2013 15:56, Jordi Boggiano a écrit :
>
>> On 10.08.2013 16:10, Simon Sapin wrote:
>>
>>> >Proposal:
>>> >
>>> >     for i in iter {
>>> >         // ...
>>> >     } else {
>>> >         // ...
>>> >     }
>>> >
>>> >The optional else block is executed when the for loop stops without
>>> >exhausting the iterator, ie. when "break" is used.
>>>
>> Maybe that's common to python devs, but I would expect else to be
>> executed if the iterator yielded nothing/was empty.
>>
>
> I agree that would also be useful, but I don’t know how to have both.
>
> It is what Jinja2 is doing. (A templating language for Python.) For what
> it’s worth, I find else-on-empty especially useful in "templates" (ie.
> generating stuff based on some data) and else-on-exhaustion especially
> useful in "data-manipulation" code.
>
>
>  IMO if you break you'll already be in a conditional of some sort in the
>> for body, so you can add more code there.
>>
>
> Yes, it’s trivial to do stuff when you break. The trick is doing stuff
> when you *don’t*. Without this proposal, the options are:
>
> * Using a boolean flag, flipped when you break
> * Avoiding the for loop entirely, using iter.next(), and doing stuff when
> receiving None.
>
> … none of which is great.
>
> --
> Simon Sapin
> ______________________________**_________________
> Rust-dev mailing list
> Rust-dev@mozilla.org
> https://mail.mozilla.org/**listinfo/rust-dev<https://mail.mozilla.org/listinfo/rust-dev>
>
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to