什么时候在 Python 中使用分号被认为是“好的”或“可接受的”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19365508/
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-19 13:34:09  来源:igfitidea点击:

When is semicolon use in Python considered "good" or "acceptable"?

python

提问by SethMMorton

Python is a "whitespace delimited" language. However, the use of semicolons areallowed. For example, the following works but is frowned upon:

Python 是一种“空格分隔”语言。然而,使用分号允许的。例如,以下工作但不赞成:

print("Hello!");
print("This is valid");

I've been using python for several years now, and the only time I have ever used semicolons is in generating one-time command-line scripts with python:

我已经使用 python 好几年了,我唯一一次使用分号是用 python 生成一次性命令行脚本:

python -c "import inspect, mymodule; print(inspect.getfile(mymodule))"

or adding code in comments on SO (i.e. "you should try import os; print os.path.join(a,b)")

或在对 SO 的评论中添加代码(即“你应该尝试import os; print os.path.join(a,b)”)

I also noticed in this answer to a similar questionthat the semicolon can also be used to make one line ifblocks, as in

我还注意到在这个对类似问题的回答中,分号也可用于制作一行if块,如

if x < y < z: print(x); print(y); print(z) 

which is convenient for the two usage examples I gave (command-line scripts and comments).

这对于我给出的两个使用示例(命令行脚本和注释)很方便。



The above examples are for communicating code in paragraph form or making short snippets, but not something I would expect in a production codebase.

上面的示例用于以段落形式交流代码或制作短片,但不是我在生产代码库中所期望的。

Here is my question: in python, is there ever a reason to use the semicolon in a production code? I imagine that they were added to the language solely for the reasons I have cited, but its always possible that Guido had a grander scheme in mind. No opinions please; I'm looking either for examples from existing code where the semicolon was useful, or some kind of statement from the python docs or from Guido about the use of the semicolon.

这是我的问题:在 python 中,是否有理由在生产代码中使用分号?我想它们被添加到语言中只是出于我所引用的原因,但 Guido 总是有可能想到一个更宏大的计划。请不要发表意见;我正在寻找现有代码中分号有用的示例,或者来自 python 文档或 Guido 的某种关于分号使用的声明。

采纳答案by BrenBarn

PEP 8is the official style guide and says:

PEP 8是官方风格指南,它说:

Compound statements (multiple statements on the same line) are generally discouraged.

通常不鼓励使用复合语句(同一行上的多个语句)。

(See also the examples just after this in the PEP.)

(另请参阅 PEP 中紧随其后的示例。)

While I don't agree with everything PEP 8 says, if you're looking for an authoritative source, that's it. You should use multi-statement lines only as a last resort. (python -cis a good example of such a last resort, because you have no way to use actual linebreaks in that case.)

虽然我不同意 PEP 8 所说的一切,但如果您正在寻找权威来源,仅此而已。您应该仅将多语句行用作最后的手段。(这python -c是这种最后手段的一个很好的例子,因为在这种情况下您无法使用实际的换行符。)

回答by Ion Freeman

I use semicolons in code all of the time. Our code folds across lines quite frequently, and a semicolon is a definitive assertion that a statement is ending.

我一直在代码中使用分号。我们的代码经常跨行折叠,分号是语句结束的明确断言。

output, errors, status = generate_output_with_errors_and_status(
    first_monstrous_functional_argument(argument_one_to_argument
        , argument_two_to_argument)
    , second_argument);

See? They're quite helpful to readability.

看?它们对可读性很有帮助。