空 while 循环的 Python 语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14797046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:27:41  来源:igfitidea点击:

Python syntax for an empty while loop

pythonwhile-loop

提问by jsj

I have written this:

我写过这个:

    while file.readline().startswith("#"):
        continue

But I suspect the continueis unnecessary? What is the correct syntax for what i'm trying to achieve?

但我怀疑这continue是不必要的?我想要实现的正确语法是什么?

采纳答案by ogzd

while file.readline().startswith("#"):
    pass

This uses the pass statement :

这使用 pass 语句:

The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action.

pass 语句什么都不做。它可以在语法上需要语句但程序不需要操作时使用。

http://www.network-theory.co.uk/docs/pytut/passStatements.html

http://www.network-theory.co.uk/docs/pytut/passStatements.html