for -- else: what was the motivation?

avi.e.gross at gmail.com avi.e.gross at gmail.com
Mon Oct 10 01:15:34 EDT 2022


Chris, a short(er) answer to your addition below.

I did not at first share your perception but maybe do now. If the argument
was that ELSE and other constructs like FINALLY or CATCH are horrible
because they follow other code and important things should be first, that is
a silly argument. Many are at the end precisely because they are not the
main code but have a role to play in making sure various conditions are
dealt with. What some of us ended up discussing was whether the advice made
much sense in a broader context and were reasonable to strive for. Many of
us say not as important as many other things, or not important at all.

I note some people looked at the way C had a FOR loop with three parts
between semicolons up front as syntactic sugar. No doubt clauses like ELSE
could in some way be moved around but the result may well violate other
sensibilities as that did because it is a rather nonstandard syntax.

I have seen languages with CASE or SWITCH statements that fall through and
allow some code to be written once and executed for several scenarios.
Others loathe the idea and want each case self-contained. There is no one
answer everyone likes. Languages are huge experiments for differing ideas.

But it is largely IRRELEVANT in that Python already made the decision and
for now it is what it is. We had a different argument a while back as to how
the word  ELSE captured what is happening linguistically and for many here
IT DOES NOT. But once you KNOW what it does, and you choose to use it, then
it works just as well as any other words because it is what it is. If having
an ELSE late disqualifies Python for this person, use another language! Of
course, PHP may not be ideal for so many other reasons!

 


-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com at python.org> On
Behalf Of Chris Angelico
Sent: Monday, October 10, 2022 12:21 AM
To: python-list at python.org
Subject: Re: for -- else: what was the motivation?

On Mon, 10 Oct 2022 at 14:59, <avi.e.gross at gmail.com> wrote:
>
> >>>Which is more disparaging: "I couldn't find anyone suggesting this" 
> >>>or
> "The only place I could find it was a PHP style guide"?
> >>>ChrisA
>
> Chris,
>
> If someone says they heard something from their own personal guru, 
> people often do not feel threatened or argue. I often am told 
> nutrition or medical or other advice that I simply ignore especially 
> if it is about exotic herbs to use or weird ideas like homeopathy or 
> that I should use language X because it is the only way to a haven or
heaven or whatever.
>
> What we had here was someone suggesting their advice was WELL-KNOWN 
> followed by lots of people sputtering about not having heard of it. I 
> actually think the advice would be reasonable in many circumstances as 
> long as it did not conflict with dozens of goals I find more 
> compelling but which vary on a case by case basis such as whether I am 
> prototyping something I will use once, ...

It's clearly not all that well known (none of us have heard of it, and it's
not exactly prominent on the internet), and it seems that most of us
disagree that it's even good advice. So, it's not really a good argument
against for-else.

> I have studied PHP but have never felt a need to use it and arguably 
> the roles it has played are often done commonly by other programs or
methods.

That's because PHP is terrible.

> So in my view, the objection is not about PHP but about uniqueness. If 
> the author of one Python textbook and no others, suggest that your 
> code should declare all imports in alphabetical order then declare all 
> functions in alphabetical order, they can not only safely be ignored, 
> but perhaps not taken seriously as imports sometimes need to be done 
> carefully if something needs something else and if a language needs 
> functions to be defined before another function body calls them, ...

It's not JUST about uniqueness. It's also that nobody but PHP programmers
seem to care about it. That's on par with going to an art supplies forum and
trying to argue that you should lay out your paints in a specific order,
because some kindergarten teacher always does it that way for the kids'
fingerpainting sessions.

No, actually, that's unfair to fingerpainting kindergarteners.

> But some people seem to miss a point we have discussed. The odd 
> clauses like ELSE after a loop (and quite a few variants in similar 
> and different cases) often provide guarantees such as making sure a file
opened is closed.

Citation needed.

> Are these things out of the way? Yes, but so what? I see things as a 
> whole not as just a single screen shot. If a loop has several optional 
> clauses lie we are discussing and you know they are normally near the 
> end, then look for them where they are.

So what's your point?

> I know some languages, JavaScript being an example, may have things 
> you might consider odd such as promoting some things like function 
> definitions found further in your code to near the top so you can use 
> a function that is not defined till later even without something like 
> a forward declaration used in other languages.

I've hardly ever seen good code that actually uses that. And when it did, it
usually wasn't deliberate. Most well-written JS code will do the same thing
that Python code does, calling things that have already been defined (if not
lexically then temporally). No hoisting needed.

> I am now going to stop replying on this topic as I have said way too 
> much and am not in particular disagreement if we are discussing 
> preferences and ideas. I see TOOLS here, not religion. Use what works 
> or that has to be used for a task but don't take any one thing too
seriously.

Yes, I see tools too. And this thread started out with a discussion of the
for-else construct, which was disparaged because it violated a rule that
nobody here has heard of, few agree with, and has exceptions already.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list