Python 绪论

本文探讨了Python编程的美学理念、明确性优于隐晦的编程原则,以及特殊情况下遵循规则的重要性。此外,还介绍了Python的保留字和魔法方法,如__new__与__init__的区别。通过实例揭示如何编写清晰简洁的代码。
摘要由CSDN通过智能技术生成

 一、作者建议

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! (命名空间是一个很棒的主意——让我们做更多的事情)
"""

二、Python 保留字

False、None、True、and、as、assert、async、await、break、class、continue、def、del、elif、else、except、finally、for、from、global、if、import、in、is、lambda、nonlocal、not、or、pass、raise、return、try、while、with、yield

三、 Python 魔法方法

# print(dir("hello"))
[
    "__add__",
    "__class__",
    "__contains__",
    "__delattr__",
    "__dir__",
    "__doc__",
    "__eq__",
    "__format__",
    "__ge__",
    "__getattribute__",
    "__getitem__",
    "__getnewargs__",
    "__gt__",
    "__hash__",
    "__init__",
    "__init_subclass__",
    "__iter__",
    "__le__",
    "__len__",
    "__lt__",
    "__mod__",
    "__mul__",
    "__ne__",
    "__new__",
    "__reduce__",
    "__reduce_ex__",
    "__repr__",
    "__rmod__",
    "__rmul__",
    "__setattr__",
    "__sizeof__",
    "__str__",
    "__subclasshook__"
]

1、__new__() & __init__()

  • __new__() 是由 object 基类提供的内置静态方法;
  • 在通过类名来创建对象的时候,python 的解释器解释器会首先调用 __new__() 分配空间并获得对象的引用;
  • 然后,将引用作为第一个参数传递给 __init__() 方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值