說說 Python 的編碼原則

執行命令:import this,就能夠看到 Tim Peters 寫的 Python 編碼原則,原文以下:less

Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than right now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!ide

翻譯以下:ui

  • 優美勝於醜陋。(代碼應該寫的漂亮而優雅。)
  • 清晰勝於晦澀。(清晰的代碼更容易維護。)
  • 簡潔勝於複雜。
  • 複雜勝於混雜。
  • 扁平勝於嵌套。
  • 間隔勝於緊湊。
  • 可讀性很重要。(即使是複雜的代碼, 也要讓它易於理解。 特別是涉及到複雜代碼時, 必定要爲這些代碼編寫有益的註釋。)
  • 即使是特例,也不能以特例之名,破壞這些規則。
  • 實用勝於純粹。
  • 不要放過錯誤,除非有明確要求。
  • 當存在多種可能時,不要猜想,而是要盡力找出一種方案,並且最好是一種顯而易見的解決方案。有時候,這並不容易,除非你是 Python 之父 Guido(由於 Guido 是荷蘭人)。
  • 立刻作也許好過從未開始,但不假思索地作有時候還不如不作(三思然後行)  。
  • 一個好的實現方案,也是容易描述的,反之亦然。
  • 命名空間是一個值得大力倡導的概念。
相關文章
相關標籤/搜索