最近在学python,今晚看了一个名叫“python全栈之路系列”的关于python的相关博客,其中开篇就说到了python的设计哲学:优雅,简洁。。。
可以在编译器里面输入如下语句来查看python语言的设计哲学:
1>>>importthis 2TheZenofPython,byTimPeters 3 4Beautifulisbetterthanugly. 5Explicitisbetterthanimplicit. 6Simpleisbetterthancomplex. 7Complexisbetterthancomplicated. 8Flatisbetterthannested. 9Sparseisbetterthandense. 10Readabilitycounts. 11Specialcasesarentspecialenoughtobreaktherules. 12Althoughpracticalitybeatspurity. 13Errorsshouldneverpasssilently. 14Unlessexplicitlysilenced. 15Inthefaceofambiguity,refusethetemptationtoguess. 16Thereshouldbeone--andpreferablyonlyone--obviouswaytodoit. 17AlthoughthatwaymaynotbeobviousatfirstunlessyoureDutch. 18Nowisbetterthannever. 19Althoughneverisoftenbetterthan*right*now. 20Iftheimplementationishardtoexplain,itsabadidea. 21Iftheimplementationiseasytoexplain,itmaybeagoodidea. 22Namespacesareonehonkinggreatidea--letsdomoreofthose!中英文释义如下:
1Beautifulisbetterthanugly. 2#优美胜于丑陋(Python以编写优美的代码为目标) 3Explicitisbetterthanimplicit. 4#明了胜于晦涩(优美的代码应当是明了的,命名规范,风格相似) 5Simpleisbetterthancomplex. 6#简洁胜于复杂(优美的代码应当是简洁的,不要有复杂的内部实现) 7Complexisbetterthancomplicated. 8#复杂胜于凌乱(如果复杂不可避免,那代码间也不能有难懂的关系,要保持接口简洁) 9Flatisbetterthannested. 10#扁平胜于嵌套(优美的代码应当是扁平的,不能有太多的嵌套) 11Sparseisbetterthandense. 12#间隔胜于紧凑(优美的代码有适当的间隔,不要奢望一行代码解决问题) 13Readabilitycounts. 14#可读性很重要(优美的代码是可读的) 15Specialcasesarentspecialenoughtobreaktherules. 16Althoughpracticalitybeatspurity. 17#即便假借特例的实用性之名,也不可违背这些规则(这些规则至高无上) 18Errorsshouldneverpasssilently. 19Unlessexplicitlysilenced. 20#不要包容所有错误,除非你确定需要这样做(精准地捕获异常,不写except:pass风格的代码) 21Inthefaceofambiguity,refusethetemptationtoguess. 22#当存在多种可能,不要尝试去猜测 23Thereshouldbeone--andpreferablyonlyone--obviouswaytodoit. 24#而是尽量找一种,最好是唯一一种明显的解决方案(如果不确定,就用穷举法) 25AlthoughthatwaymaynotbeobviousatfirstunlessyoureDutch. 26#虽然这并不容易,因为你不是Python之父(这里的Dutch是指Guido) 27Nowisbetterthannever. 28Althoughneverisoftenbetterthan*right*now. 29#做也许好过不做,但不假思索就动手还不如不做(动手之前要细思量) 30Iftheimplementationishardtoexplain,itsabadidea. 31Iftheimplementationiseasytoexplain,itmaybeagoodidea. 32#如果你无法向人描述你的方案,那肯定不是一个好方案;反之亦然(方案测评标准) 33Namespacesareonehonkinggreatidea--letsdomoreofthose! 34#命名空间是一种绝妙的理念,我们应当多加利用(倡导与号召)然后,我尝试了里面的一段demo代码:
1#coding=utf-82print(""" 3mynameiszhangweigong 4myblogsurlis:www.cnblogs.com/imyalost 5lifeissoshort,Ineedpython 6""")运行结果是可以成功运行的,但打印出来的结果前面,专门提及了python的设计哲学,说明,这段代码是不够简洁高效的。。。
突然明白一个道理:学习一门编程语言,一定要了解这门语言的特性。。。
其实做一件事学一门技术也一样(不限于编程语言,虽然编程语言也是一门技术、手段),要了解它的特性,才能更好的使用它,发挥它的作用!!!
引以为戒啊,少年。。。
本文内容总结:
原文链接:https://www.cnblogs.com/imyalost/p/7414264.html