Skip to Content
流畅的Python(第2版)
book

流畅的Python(第2版)

by Luciano Ramalho
April 2023
Intermediate to advanced
769 pages
25h 16m
Chinese
Posts & Telecom Press
Content preview from 流畅的Python(第2版)
运算符重载
451
的过程中,我们得知,在可接受的类型方面,
+
应该比
+=
严格。对于序列类型,
+
通常要
求两个操作数属于同一类型,而
+=
的右侧操作数往往可以是任何可迭代对象。
16.11
 延伸阅读
Guido van Rossum
写了一篇文章为运算符重载辩护,题为“
Why operators are useful
”。
Trey
Hunner
在“
T
uple ordering and deep comparisons in Python
”一文中指出,
Python
提供的众
多比较运算符比很多来自其他编程语言的程序员想象中更灵活且更强大。
Python
编程中
,运算符重载经常使用
isinstance
做测试。这种测试充分利用了大鹅类
型(详见
13.5
节)
。如果你跳过了那一节,请务必翻回去阅读。
运算符特殊方法的主要参考资料是《
Python
语言参考手册》中的第
3
章。
Python
标准库文档
numbers
模块文档中的“
9.1.2.2. Implementing the arithmetic operations
”一节也值得一读。
Python 3.4
增加的
pathlib
包就灵活利用了运算符重载。
Path
类重载了
/
运算符,根据字
符串构建文件系统路径,如文档中下面这个示例所示。
>>> p = Path('/etc')
>>> q = p / 'init.d' / 'reboot'
>>> q
PosixPath('/etc/init.d/reboot')
在非算术运算符方面,用于“发送、嗅探、剖析和伪造网络分组”的 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python高级编程(第2版)

Python高级编程(第2版)

Posts & Telecom Press, Michał Jaworski, Tarek Ziadé
Kafka权威指南(第2版)

Kafka权威指南(第2版)

Gwen Shapira, Todd Palino, Rajini Sivaram, Krit Petty
Python贝叶斯分析(第2版)

Python贝叶斯分析(第2版)

Posts & Telecom Press, Osvaldo Martin

Publisher Resources

ISBN: 9787115612366