像這種…我都不覺得python風格很好,閱讀和書寫都不方便,現在編輯器這麼方便,分號都能自動完成。大家都覺得Python風格很好嗎?
(圖片來源:https://twitter.com/thedirtycoder/status/569339014085517312
)
回複內容:
貼一段 Learning Python, 5th Edition - O'Reilly Media
好了:
Why Indentation Syntax?
The indentation rule may seem unusual at first glance to programmers accustomed toC-like languages, but it is a deliberate feature of Python, and it’s one of the main waysthat 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 andreadable (unlike much of the code written in C-like languages).
To put that more strongly, aligning your code according to its logical structure is amajor part of making it readable, and thus reusable and maintainable, by yourself andothers. In fact, even if you never use Python after reading this book, you should get intothe habit of aligning your code for readability in any block-structured language. Pythonunderscores the issue by making this a part of its syntax, but it’s an important thing todo in any programming language, and it has a huge impact on the usefulness of yourcode.
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 bymany programmers over the years. Almost invariably, each programmer had his or herown style for indenting code.
強制縮排其實是Python保持代碼風格統一且可讀性良好的機制。
同時並不真誠的祝願那些代碼風格寫的跟狗屁一樣還覺得自己又屌又機智又個性的同學將來不被合作的同事打飛。貼的圖是Java啊。。。你個java用什麼python式的縮排啊……
我用
純正的python 改寫一下你的代碼:
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這麼做好不好的問題。
主流IDE都有自動排版功能,你試一下就知道了。補充一句,我是指其他語言的自動排版。Python的自動排版不能處理語句開始的自動縮排。只能改一下字元間的距離。python這種東西就是強制叫你縮排正常化。如果你遵循規範,python這種東西直接省掉了大括弧分號,沒有其他習慣上的改變。如果你壓根無視縮排規範…抱歉我真不想看沒縮排的代碼,而且我相信我不是一個人。
至於說寫的時候不縮排寫完再自動排縮排什麼的…寫個複雜點的東西估計查錯都要抓狂吧…
話說貼個java圖已經被吐槽很多了可還是忍不住吐槽…你好歹貼個文法像一點兒的啊…你…貼…個…java…幹…嘛…縮排解決了花括弧要不要換行寫的問題。
現在編輯器這麼方便,分號都能自動完成。
連分號都不想寫,那還不用Python?直接不寫嘛。:-D不是很好大神們已經用也許“蹩腳”的語言披荊斬棘,你tm還在討論這些??反正都要換行縮排加括弧,
少按一次鍵盤,並且自動對齊不是很好麼