Python:使用4个空格进行缩进。为什么? [英] Python: using 4 spaces for indentation. Why?

查看:742
本文介绍了Python:使用4个空格进行缩进。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在对python进行编码时,我仅使用2个空格进行缩进,请确保 PEP-8 真的建议您使用4个空格,但从历史上看,这是不寻常的。

While coding python I'm using only 2 spaces to indent, sure PEP-8 really recommend to have 4 spaces, but historically for me it's unusual.

因此,有人可以说服我使用4个空格而不是2个吗?

So, can anyone convince me to use 4 spaces instead of 2? What pros and cons?

P.S。最后,将所有现有代码库从2个空间转换为4个空间的简单方法是什么?

P.S. And finally, what's easy way to convert all existing codebase from 2 spaces to 4 spaces?

P.P.S。 PEP-8还严格建议不要使用制表符进行缩进。 在此处阅读

P.P.S. PEP-8 Also srictly recommend not using tabs for indention. read here

所以,总结一下:

优点:


  • 在包装长度超过80行的字符串时有更多空间可以布置。

  • 可以从摘要中复制代码,

缺点:


  • 嵌套语句的层次越深,实际代码的空间就越小。

谢谢。

推荐答案

其他每个人都使用4个空格。这是使用我遇到并接受的4个空格的唯一原因。我内心仍然想使用制表符(每个缩进1个缩进字符,很有意义,不是吗?将缩进与其他空格分开。我不在乎制表符可以显示为不同的宽度,没有语法上的差异。最糟糕的情况是某些注释不能对齐。这很恐怖!)但是我已经接受了,因为python社区整体使用4个空格,所以我使用4个空格。这样,我就可以从其他人编写的摘录中汇编代码,并且一切正常。

Everyone else uses 4 spaces. That is the only reason to use 4 spaces that I've come across and accepted. In my heart, I still want to use tabs (1 indent character per indent, makes sense, no? Separate indent from other whitespace. I don't care that tabs can be displayed as different widths, that makes no syntactic difference. The worst that can happen is that some of the comments don't line up. The horror!) but I've accepted that since the python community as a whole uses 4 spaces, I use 4 spaces. This way, I can assemble code from snippets others have written, and it all works.

这篇关于Python:使用4个空格进行缩进。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆