Op 3/03/2022 om 14:24 schreef computermaster360:
I want to make a little survey here.

Do you find the for-else construct useful? Have you used it in
practice? Do you even know how it works, or that there is such a thing
in Python?
- No, or at least not when balanced against the drawbacks as I perceive them. - I have a hard time remembering how it works, which is the major drawback for me. I simply can't seem to wrap my head around it.
- Yes
I have used it maybe once. My issue with this construct is that
calling the second block `else` doesn't make sense; a much more
sensible name would be `then`.
There have been multiple proposals for a better name, but they all have their own drawbacks. There doesn't seem to be a clear winner.

Maybe a bit of an explanation of my dislike for the for-else construct. I can't remember what exactly it does and doesn't do, maybe because of the IMHO unfortunate naming or maybe because it's just an unusal construct, I don't know. While coding I never feel the need for for-else. I like coding in a way where "every line expresses a single thought", as much as possible. That means that I don't mine creating short functions for things that in my mind otherwise break the flow of my code. So I often have short functions with a for loop and early returns. I have never seen an example of for-else that wouldn't be at least as clear with a little helper function (granted I haven't looked all that hard).

I'm googling for an example, but all I find are tutorial-type things that don't necessarily compare to real code. But it's the best I can do, so let's dive in. What they all seem to have in common is that they mix business logic and user interface (maybe precisely because of their tutorial nature), which I strive to avoid. For example, on https://www.geeksforgeeks.org/using-else-conditional-statement-with-for-loop-in-python/ I found this example:

    # Python 3.x program to check if an array consists
    # of even number
    def contains_even_number(l):
      for ele in l:
        if ele % 2 == 0:
          print ("list contains an even number")
          break

      # This else executes only if break is NEVER
      # reached and loop terminated after all iterations.
      else:
        print ("list does not contain an even number")

    # Driver code
    print ("For List 1:")
    contains_even_number([1, 9, 8])
    print (" \nFor List 2:")
    contains_even_number([1, 3, 5])

I would always refactor this even just for that mixing of business logic and printing stuff. Perhaps like this:

    def contains_even_number(lst):
        for element in lst:
            if element % 2 == 0:
                return True
        return False

    def print_contains_even_number(lst):
        if contains_even_number(lst):
            print("list contains an even number")
        else:
            print("list does not contain an even number")

    print("For List 1:")
    print_contains_even_number([1, 9, 8])
    print()
    print("For List 2:")
    print_contains_even_number([1, 3, 5])

Even without consciously avoiding for-else, it automatically disappears.
(In real code I would actually use any(element % 2 == 0 for element in lst) for something simple like this)

Now, imagine a parallel universe, where the for-else construct would
have a different behavior:

     for elem in iterable:
         process(elem)
     else:
         # executed only when the iterable was initially empty
         print('Nothing to process')

Wouldn't this be more natural? I think so. Also, I face this case much
more often than having detect whether I broke out of a loop early
(which is what the current for-else construct is for).
That's a different construct, also possibly useful. I think this would be more useful to me than the existing for-else.

--
"Man had always assumed that he was more intelligent than dolphins because
he had achieved so much — the wheel, New York, wars and so on — whilst all
the dolphins had ever done was muck about in the water having a good time.
But conversely, the dolphins had always believed that they were far more
intelligent than man — for precisely the same reasons."
        -- Douglas Adams

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

Reply via email to