我在兩個月前的鐵人賽中寫了Django與Rails的比較,現在看來有些許幼稚之處。轉念一想,也是因為寫出來才有辦法痛改前非,算是逼自己成長的方法之一。
討論到Python的精神中,有一首很有名的新詩(?)The Zen of Python:
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!
-- by Tim Peters
網路上解釋這首充滿「禪意」的詩很多,我今天想特別提出來討論的是「Explicit is better than implicit」,如果翻為中文可以理解為「明示勝於暗示」。這同時也在Django官方文件中,專門解釋設計哲學的地方特別提到:
This is a core Python principle listed in PEP 20, and it means Django shouldn’t do too much “magic.” Magic shouldn’t happen unless there’s a really good reason for it. Magic is worth using only if it creates a huge convenience unattainable in other ways, and it isn’t implemented in a way that confuses developers who are trying to learn how to use the feature.
從這點去理解,就知道為什麼Python不喜歡像是Rails裡常用的「黑魔法」,即使相當便捷誘人。有鑑於此,批評Python或是Django不如Ruby on Rails一般「有如神助」,我覺得是對他們的語言精神的不夠尊重,在這點上只能說精神立意不同,端看工程師如何取捨。
至於核心精神的好壞,這可能需要更多年的廝殺才會有結論的,我也不敢妄下定論了。