Why avoid while loops?

后端 未结 13 1143
失恋的感觉
失恋的感觉 2020-12-09 15:24

I\'m about 2 weeks deep in my study of Python as an introductory language. I\'ve hit a point in Zed\'s \"Learn Python the Hard Way\" where he suggests:

相关标签:
13条回答
  • 2020-12-09 16:07

    It is not a speed issue, and it is what it is, a suggestion.

    I think the intention is to make the code more explicit and easier to read. If you can write it without a while loop in python, it probably looks cleaner and is easier to understand.

    0 讨论(0)
  • 2020-12-09 16:12

    The advice seems poor to me. When you're iterating over some kind of collection, it is usually better to use one of Python's iteration tools, but that doesn't mean that while is always wrong. There are lots of cases where you're not iterating over any kind of collection.

    For example:

    def gcd(m, n):
        "Return the greatest common divisor of m and n."
        while n != 0:
            m, n = n, m % n
        return m
    

    You could change this to:

    def gcd(m, n):
        "Return the greatest common divisor of m and n."
        while True:
            if n == 0:
                return m
            m, n = n, m % n
    

    but is that really an improvement? I think not.

    0 讨论(0)
  • 2020-12-09 16:12

    A while loop is a tool, nothing more. To use it or not use it is a matter of whether it's the right tool for the job, nothing more. You are right to question the advice, it's silly.

    That being said, there are constructs in python that obviate the need for while loops some of the time. For example, list comprehensions do things one might ordinarily do with a while or for loop.

    Use the right tool for the job, don't try to memorize and stick to a bunch of rules. The goal is to write code that a) works and b) is clear.

    0 讨论(0)
  • 2020-12-09 16:13

    Python follows the philosophy of

    There should be one-- and preferably only one --obvious way to do it.

    (see https://www.python.org/dev/peps/pep-0020/ for details).

    And in most cases there is a better way to do iterations in Python than using a while loop.

    Additionally at least CPython optimizes other kinds of loops and iterations (I consider map and list comprehensions as kinds of iteration) better than while loops.

    Example: https://www.python.org/doc/essays/list2str

    Use intrinsic operations. An implied loop in map() is faster than an explicit for loop; a while loop with an explicit loop counter is even slower.

    I have no explanation why you were told to use while loops instead of for loops in VB - I think it normally leads neither to better readable nor to faster code there.

    0 讨论(0)
  • 2020-12-09 16:13

    Also there are list comprehensions e.g.:
    >>> vec = [2, 4, 6]
    >>> [3*x for x in vec]
    [6, 12, 18]

    0 讨论(0)
  • 2020-12-09 16:15

    The general issue with while loops is that it is easy to overlook, or somehow skip the statement where the loop variable gets incremented. Even a C for-loop can have this problem - you can write for(int x=0; x<10;), although it is unlikely. But a Python for loop has no such problem, since it takes care of advancing the iterator for you. On the other hand, a while loop forces you to implement a never fail increment.

    s = "ABCDEF"
    i = 0
    while (i < len(s)):
        if s[i] in "AEIOU":
            print s[i], 'is a vowel'
            i += 1
    

    is an infinite loop as soon as you hit the first non-vowel.

    0 讨论(0)
提交回复
热议问题