码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Python之禅——跟老吕学Python编程


    Python之禅——跟老吕学Python编程

    • Python之禅
      • 1.**Beautiful is better than ugly.**
      • 2.**Explicit is better than implicit.**
      • 3.**Simple is better than complex.**
      • 4.**Complex is better than complicated.**
      • 5.**Flat is better than nested.**
      • 6.**Spare is better than dense.**
      • 7.**Readability counts.**
      • 8.**Special cases aren’t special enough to break the rules.**
      • 9.**Although practicality beats purity.**
      • 10.**Errors should never pass silently.**
      • 11.**Unless explicitly silenced.**
      • 12.**In the face of ambiguity , refuse the temptation to guess.**
      • 13.**There should be one – and preferably only one – obvious way to do it.**
      • 14.**Although that way may not be obvious at first unless you’re Dutch.**
      • 15.**Now is better than never.**
      • 16.**Although never is often better than right now.**
      • 17.**If the implementation is hard to explain , it’s a bad idea.**
      • 18.**If the implementation is easy to explain , it’s a good idea.**
      • 19.**Namespaces are one honking great idea – let’s do more of those !**

    Python之禅


    在Python解释器中输入“import this”会发生什么?

    在Python IDLE Shell窗口中输入Python代码 import this 会有一段 “Python之禅” 格言,作者是内核开发者Tim Peters,Guido 叔的 Python 语言设计指导原则浓缩为了 19 条开发哲学。

    源代码如下:

    import this
    
    • 1

    Python之禅——跟老吕学Python编程

    展示Tim Peters的The Zen of Python “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!
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    Python的理念都包含在Tim Peters撰写的 Python之禅 —— “import this” 中的指导原则对于我们是很重要的,能给予我们很大的帮助。



    1.Beautiful is better than ugly.

    优美胜于丑陋

    Python 以编写优美的代码为目标



    2.Explicit is better than implicit.

    清晰明了胜过晦涩难懂

    优美的代码应当是明了的,命名规范,风格相似



    3.Simple is better than complex.

    简洁胜于复杂

    优美的代码应当是简洁的,不要有复杂的内部实现



    4.Complex is better than complicated.

    复杂胜于凌乱

    如果复杂不可避免,那代码间也不能有难懂的关系,要保持接口简洁



    5.Flat is better than nested.

    扁平胜于嵌套

    优美的代码应当是扁平的,不能有太多的嵌套



    6.Spare is better than dense.

    间隔胜于紧凑

    优美的代码有适当的间隔,不要奢望一行代码解决问题



    7.Readability counts.

    可读性很重要

    优美的代码是可读的



    8.Special cases aren’t special enough to break the rules.

    没有规矩,不成方圆

    特例不足以打破这些原则



    9.Although practicality beats purity.

    实践打破真理

    任何理论都需要经过实践的检验。



    10.Errors should never pass silently.

    错误永远不该被静默地传递下去

    遇到错误,应认真的面对解决,而不是悄无声息的容忍放过它



    11.Unless explicitly silenced.

    除非必须这样做

    要能够精确的捕获异常,处理异常。



    12.In the face of ambiguity , refuse the temptation to guess.

    面对模棱两可的答案,不要试图猜测

    处理问题可以采用不同的方式解决,不要因为自己和别人的方式不同而质疑自己。



    13.There should be one – and preferably only one – obvious way to do it.

    显而易见的是真理只有一个

    一个程序让两个不同的人去编写,方案大致是一致的,并非没有创意空间,恰恰相反,大部分的工作使用常见的简单的方案解决。



    14.Although that way may not be obvious at first unless you’re Dutch.

    万事开头难,因为你不是Python之父(这里的Dutch是指Guido)

    学Python一开始都是困难的,而你又不是Python之父(这里的Dutch是指Python之父),坚持学习才能够学好Python。



    15.Now is better than never.

    现在开始做也为时未晚,远胜于永不开始做

    学习的道路是从现在开始从来都不会晚,要相信学到老学到老,一直坚持学习才能够提升自己。



    16.Although never is often better than right now.

    做也许好过不做,但不假思索就动手还不如不做(动手之前要细思量)

    “学而不思则罔”,学习需要思考,每次写一个程序一起以前需要经过一定的考虑,才开始写,会减少很多不必要的错误。



    17.If the implementation is hard to explain , it’s a bad idea.

    如果方案难以被解释清楚,那么一定是个烂方案

    Python编程是简单而且直接的,如果你都没想明白整个程序的合理编写思路,那么还不如不要动手去写了。



    18.If the implementation is easy to explain , it’s a good idea.

    如果方案实现很容易解释,那么一定是个好方案

    Python编程是简单而且直接的,如果你要编写的整个程序的思路清晰合理,那么你将会把这个方案做的很好。



    19.Namespaces are one honking great idea – let’s do more of those !

    命名空间是一种绝妙的理念,我们应当多加利用(倡导与号召)

    命名空间是一个好的理念,善于运用



    如果您觉得本文有帮助,辛苦您点个赞、收个藏、或评论一句,您的举手之劳将对我提供了无限的写作动力!
    博主ID:Python老吕,希望大家点赞、评论、收藏。


  • 相关阅读:
    外包干了3个月,技术倒退2年。。。
    xilinx fpga ultrascale 器件GTX参考时钟注意点
    SSH协议&在IDEA中使用Git及git GUI的简单使用
    车载多通道语音识别挑战赛(ICMC-ASR)丨ICASSP2024
    深度学习 | Pytorch深度学习实践 (Chapter 10、11 CNN)
    javaSE - Arrays - 数组的定义与使用
    【实验记录】一些小疑问
    人工智能、机器学习、深度学习:技术革命的深度解析
    数据结构与算法分析——数学基础(为之后的算法分析打基础)
    网络相关知识
  • 原文地址:https://blog.csdn.net/molangmolang/article/details/136626038
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号