简体   繁体   English

python 中单行语句的目的是什么?

[英]What is the purpose of single-line statements in python?

I am aware that multiple lines in python that are part of a conditional statement can be reduced into a single line.我知道作为条件语句一部分的 python 中的多行可以简化为一行。

For example:例如:

if foo == 'bar':
   do_one()
   do_two()
   do_three()

Can be reduced down to:可以简化为:

if foo == 'bar': do_one(); do_two(); do_three()

But what are the advantages of such a syntax apart from the obvious fact that it takes up fewer lines of code?但是除了占用更少的代码行这一显而易见的事实之外,这种语法还有什么优势呢?

eg Is it more memory efficient?例如,memory 效率更高吗?

Is not the standard syntax much more readable for the average Python user?对于普通 Python 用户来说,标准语法不是更具可读性吗?

AFAIK, there are no advantages of compound statements. AFAIK,复合语句没有优势。 It is actually even worse to do that as it make your code less readable.这样做实际上更糟糕,因为它会降低您的代码的可读性。 In most cases you write your code so that it is easy for humans to read it rather than to make it too complicated or short.在大多数情况下,您编写代码是为了便于人们阅读,而不是让它太复杂或太短。

You can find some more infromation here你可以在这里找到更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM