Why avoid while loops?

后端 未结 13 1172
失恋的感觉
失恋的感觉 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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.

提交回复
热议问题