Python is a "whitespace delimited" language. However, the use of semicolons are allowed. 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的评论中添加代码(即“你应该尝试导入os;打印os.path.join(a,b)”)
I also noticed in this answer to a similar question that the semicolon can also be used to make one line if
blocks, as in
我在这个答案中也注意到了一个类似的问题,即分块也可以用来制作一行,就像块一样
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 docs或Guido关于分号使用的某种语句。
2 个解决方案
#1
16
PEP 8 is 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 -c
is a good example of such a last resort, because you have no way to use actual linebreaks in that case.)
虽然我不同意PEP 8所说的一切,但如果你正在寻找一个权威的来源,就是这样。您应该仅使用多语句行作为最后的手段。 (python -c是这种最后手段的一个很好的例子,因为在这种情况下你无法使用实际的换行符。)
#2
3
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.
看到?它们对可读性非常有帮助。
#1
16
PEP 8 is 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 -c
is a good example of such a last resort, because you have no way to use actual linebreaks in that case.)
虽然我不同意PEP 8所说的一切,但如果你正在寻找一个权威的来源,就是这样。您应该仅使用多语句行作为最后的手段。 (python -c是这种最后手段的一个很好的例子,因为在这种情况下你无法使用实际的换行符。)
#2
3
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.
看到?它们对可读性非常有帮助。