时间:2021-07-01 10:21:17 帮助过:55人阅读
强制缩进其实是Python保持代码风格统一且可读性良好的机制。Why Indentation Syntax?
The indentation rule may seem unusual at first glance to programmers accustomed to C-like languages, but it is a deliberate feature of Python, and it’s one of the main ways that Python almost forces programmers to produce uniform, regular, and readable code. It essentially means that you must line up your code vertically, in columns, ac- cording to its logical structure. The net effect is to make your code more consistent and readable (unlike much of the code written in C-like languages).
To put that more strongly, aligning your code according to its logical structure is a major part of making it readable, and thus reusable and maintainable, by yourself and others. In fact, even if you never use Python after reading this book, you should get into the habit of aligning your code for readability in any block-structured language. Python underscores the issue by making this a part of its syntax, but it’s an important thing to do in any programming language, and it has a huge impact on the usefulness of your code.
Your experience may vary, but when I was still doing development on a full-time basis, I was mostly paid to work on large old C++ programs that had been worked on by many programmers over the years. Almost invariably, each programmer had his or her own style for indenting code.
class Permuter:
def permute(self, n, a):
if n==0:
print a
else:
for i in range(n+1):
permute(n-1, a)
self.swap(a, 0 if n%2 else i, n)
def swap(self, a, i, j):
a[i], a[j] = a[j], a[i]
其实任何语言在编写的时候都会把缩进作为一种规范。即使缩进对于编译器没有实际意义,但是却增大了可读性。所以既然有没有用都要缩进,也就不存在Python这么做好不好的问题。现在编辑器这么方便,分号都能自动完成。
连分号都不想写,那还不用Python?直接不写嘛。:-D
不是很好
大神们已经用也许“蹩脚”的语言披荆斩棘,你tm还在讨论这些??
反正都要换行缩进加括号,