2010-10-15 77 views
3

如果你谷歌的「pythonic」,你會發現主要是the same three examples。這裏有很多關於stackoverflow的問題,要求如何在pythonoic的方式下完成這個和那個,所以一些不錯的pythonic代碼示例的集合將會很好!你會認爲什麼做法是「pythonic」?

+3

投票結束:這裏沒有真正的問題。 – 2010-10-15 13:57:34

+0

http://ideone.com/kMEuL – 2010-10-15 14:07:07

+0

如果它是可讀**和**產生預期的輸出,它是Pythonic。 – tzot 2010-10-15 19:14:04

回答

5

我在tag description寫道:

Pythonic是最地道的Python代碼的說明。這不僅意味着代碼對於其他程序員來說很容易理解,而且它也是使用Python最有效的方式。

2
>>> import this 
The Zen of Python, by Tim Peters 

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! 
0

「Python化」 只是意味着以下常見的Python成語。

只需按照禪宗的Python

  • 美麗的比醜好。
  • 顯式優於隱式。
  • 簡單勝過複雜。
  • 複雜比複雜好。
  • Flat比嵌套更好。
  • 稀疏比密集好。
  • 可讀性計數。
  • 特殊情況不足以破壞規則。
  • 雖然實用性勝過純度。
  • 錯誤不應該默默通過。
  • 除非明確沉默。
  • 面對歧義,拒絕猜測的誘惑。
  • 應該有一個 - 最好只有一個 - 明顯的方法來做到這一點。
  • 雖然這種方式可能並不明顯,除非你是荷蘭人。
  • 現在總比從未好。
  • 雖然從來沒有比現在更好。
  • 如果執行很難解釋,這是一個壞主意。
  • 如果實施很容易解釋,這可能是一個好主意。
  • 命名空間是一個重要的想法 - 讓我們做更多的這些!
2

的問題問的Python的代碼的集合,所以我會添加一些人,我喜歡,因爲他們使用的是強大的Python的運營商***

換位使用拆包經營者*

解包運算符是一個非常強大的工具,可以讓我們「解包」一個列表。我不知道其他語言的任何等價物。

此操作可以有非常有趣和有用的應用程序:再次

a = [[1,2],[3,4]] 
a_transpose = zip(*a) 

Dictionnary級聯使用的元組拆包經營者**

,我不知道在其他語言中的任何等價物。和上面一樣,我們可以使用這個操作符來處理許多事情,包括字典串聯:

a = {1:2,2:2} 
b = {2:37,3:42} 
a = dict(a,**b) # a is now {1:2,2:37,3:42} 
相關問題