for -- else: what was the motivation?

Chris Angelico rosuav at gmail.com
Mon Oct 10 00:20:40 EDT 2022


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


More information about the Python-list mailing list