for -- else: what was the motivation?

Chris Angelico rosuav at gmail.com
Sun Oct 9 01:15:29 EDT 2022


On Sun, 9 Oct 2022 at 16:05, Axy via Python-list <python-list at python.org> wrote:
>
>
> On 09/10/2022 05:47, Chris Angelico wrote:
> > On Sun, 9 Oct 2022 at 15:39, Axy via Python-list <python-list at python.org> wrote:
> >> Got it, thanks!
> >>
> >> Actually the reason I never used "else" was the violation of the rule of
> >> beauty "shortest block first". With if--else you can easily follow this
> >> rule by inverting "if" expression, but with for--else you can't. The
> >> loop body of the simplest example is already three lines, in real life
> >> things are much worse.
> >>
> > That's not a rule I've ever been taught; how important is it?
> >
> > ChrisA
>
> It gets important if the lifetime of your project is more than three
> months and is extremely important if more than 10 years. But, it depends.

Yes, I'm aware that code readability becomes irrelevant for
short-duration projects. Beside the point. I'm wondering how important
it really is to have the shortest block first.

> I also might be wrong in terminology, anyway, there are many rules that
> make programmer's life easier, described in the literature from the old
> good "How to write unmaintainable code" to "The Art of Readable Code".
> And I hope there are a lot of recent books on this subject I did not
> track and read yet.

Also not really a justification for "shortest block first". Wanting
some elaboration on that. What's the value in it?

Given that for-else is an excellent, if rarely-used, construct, I
would say that, *at least*, it is worth setting aside this rule for
that particular situation. It is also generally worth using fewer
commas than I just did. Take my advice with a grain of salt.

ChrisA


More information about the Python-list mailing list