简体   繁体   English

Python空白空间样式指南

[英]Python White Spaces Style Guide

The Python Style Guide recommends not putting white spaces in expressions and statements. 《 Python样式指南》建议不要在表达式和语句中放置空格。 So, when I do these: 因此,当我执行这些操作时:

def testme( first_arg, second_arg ):
    pass

testme( 'a', 'b' )

and run pep8 against it, it will complain about the white spaces. 并对其运行pep8,它将抱怨空白。

I know this is just a style guide so it should not affect the code but I want to understand the logic behind this style. 我知道这只是样式指南,所以它不会影响代码,但我想了解这种样式背后的逻辑。 To me, the white spaces gives more clarity. 对我来说,白色空格更加清晰。 Is this something universally accepted in most programming language? 这是大多数编程语言中普遍接受的东西吗?

This is style, something that is an opinion. 这是风格,是一种见解。 Wherever an opinion exists, people will disagree. 无论哪里有意见,人们都会不同意。 That is fine and that is how it works. 很好,就是这样。

But there's more. 但是还有更多。 This is code - it's not only meant to be read by the next person, he/she will most likely also be editing the code. 这是代码-不仅是要由下一个人阅读,他/她很可能还将在编辑代码。

And everything goes fine - until two people with different styles edit the same code that is. 一切顺利-直到两个具有不同样式的人编辑相同的代码为止。 Everything will look different, the code will be confusing and bugs will start appearing. 一切看起来都会不同,代码会令人困惑,并且错误会开始出现。

But there's more. 但是还有更多。 In Python whitespace is semantic , if you start mixing 4 spaces with tabs everything goes wrong. 在Python中,空白是语义上的 ,如果您开始将4个空格与制表符混合使用,一切都会出错。 So having different styles in the same file (and same project for that matter) is even worse in Python than in most languages. 因此,在Python中比在大多数语言中,在同一文件(和同一项目)中具有不同的样式甚至更糟。

For this reason PEP 8 was created, an attempt to create an universal style guide for Python. 因此,创建了PEP 8,试图为Python创建通用样式指南。 It is of course your choice whether or not to follow this guide, but it is recommended. 当然,您可以选择是否遵循本指南,但建议您这样做。

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

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