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版)
646
22
实现为内置函数
dir()
提供支持的
__dir__
方法,进而支持在
Python
标准控制台,以及
IPython
Jupyter
Notebook
等中自动补全。这个方法的代码很简单,将基于
self.__data
中的键实现递归自动补全,因为
__getattr__
方法能即时构建
FrozenJSON
实例——方便
采用交互方式探索数据。
这是一个备选构造方法,是
@classmethod
装饰器的常见用途。
如果
obj
是一个映射,那么就构建一个
FrozenJSON
对象。这里利用了
大鹅类型
(详见
13.5
)。
如果是一个
MutableSequence
对象,则必然是列表,
6
因此,把
obj
中的每一项递归都传
.build()
方法,构建一个列表。
如果既不是字典也不是列表,那么原封不动返回项。
FrozenJSON
实例的私有实例属性
__data
存储在
_FrozenJSON__data
名下(详见
11.10
)。
尝试通过其他名称获取属性将触发
__getattr__
方法。这个方法会先查看
self.__data
典有没有指定名称的属性(不是键),这样
FrozenJSON
实例便可以处理
dict
的方法,例
如把
items
方法委托给
self.__data.items()
。如果
self.__data
没有指定名称的属性,那
__getattr__
方法就会以那个名称为键,从
self.__data
中获取一项,传给
FrozenJSON.
build
方法。这样便能深入
JSON
数据的嵌套结构,使用类方法 ...
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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Python高级编程(第2版)

Python高级编程(第2版)

Posts & Telecom Press, Michał Jaworski, Tarek Ziadé
Python贝叶斯分析(第2版)

Python贝叶斯分析(第2版)

Posts & Telecom Press, Osvaldo Martin
高效能PYTHON程式設計

高效能PYTHON程式設計

Micha Gorelick, Ian Ozsvald

Publisher Resources

ISBN: 9787115612366