Skip to Content
For Enterprise
For Government
For Higher Ed
For Individuals
For Marketing
For Enterprise
For Government
For Higher Ed
For Individuals
For Marketing
Explore Skills
Cloud Computing
Microsoft Azure
Amazon Web Services (AWS)
Google Cloud
Cloud Migration
Cloud Deployment
Cloud Platforms
Data Engineering
Data Warehouse
SQL
Apache Spark
Microsoft SQL Server
MySQL
Kafka
Data Lake
Streaming & Messaging
NoSQL Databases
Relational Databases
Data Science
Pandas
R
MATLAB
SAS
D3
Power BI
Tableau
Statistics
Exploratory Data Analysis
Data Visualization
AI & ML
Generative AI
Machine Learning
Artificial Intelligence (AI)
Deep Learning
Reinforcement Learning
Natural Language Processing
TensorFlow
Scikit-Learn
Hyperparameter Tuning
MLOps
Programming Languages
Java
JavaScript
Spring
Python
Go
C#
C++
C
Swift
Rust
Functional Programming
Software Architecture
Object-Oriented
Distributed Systems
Domain-Driven Design
Architectural Patterns
IT/Ops
Kubernetes
Docker
GitHub
Terraform
Continuous Delivery
Continuous Integration
Database Administration
Computer Networking
Operating Systems
IT Certifications
Security
Network Security
Application Security
Incident Response
Zero Trust Model
Disaster Recovery
Penetration Testing / Ethical Hacking
Governance
Malware
Security Architecture
Security Engineering
Security Certifications
Design
Web Design
Graphic Design
Interaction Design
Film & Video
User Experience (UX)
Design Process
Design Tools
Business
Agile
Project Management
Product Management
Marketing
Human Resources
Finance
Team Management
Business Strategy
Digital Transformation
Organizational Leadership
Soft Skills
Professional Communication
Emotional Intelligence
Presentation Skills
Innovation
Critical Thinking
Public Speaking
Collaboration
Personal Productivity
Confidence / Motivation
Features
All features
Verifiable skills
AI Academy
Courses
Certifications
Interactive learning
Live events
Superstreams
Answers
Insights reporting
Radar Blog
Buy Courses
Plans
Sign In
Try Now
O'Reilly Platform
book
流畅的Python(第2版)
by
Luciano Ramalho
April 2023
Intermediate to advanced
769 pages
25h 16m
Chinese
Posts & Telecom Press
Content preview from
流畅的Python(第2版)
305
第
12
章
序列的特殊方法
不要通过
叫声
、
走路姿势
等像不像鸭子来检查它
是不是
鸭子,具体检查什么取决
于你想使用语言的哪些行为。(
comp.lang.python
,
2000
年
7
月
26
日
。)
——
Alex Martelli
本章将以第
11
章定义的二维向量类
Vector2d
为基础,向前迈出一大步,定义表示多维向
量的
Vector
类。这个类的行为与
Python
中标准的不可变扁平序列一样。
Vector
实例中的
元素是浮点数,本章结束后
Vector
类将支持以下功能。
•
基本的序列协议:
__len__
和
__getitem__
。
•
正确表述拥有很多元素的实例。
•
适当的切片支持,生成新的
Vector
实例。
•
综合各个元素的值计算哈希值。
•
自定义的格式语言扩展。
此外,本章还将通过
__getattr__
方法实现属性的动态存取,以取代
Vector2d
使用的只读
特性——不过,序列类型通常不会这么做。
在大量代码之间,本章将穿插讨论一个概念:把协议当作正式接口。我们将说明协议和
鸭
子类型
之间的关系,以及对自定义类型的实际影响。
12.1
本章新增内容
本章没有太大的变化,
12.4
节在结尾处新增了一个“提示栏”,简单讨论
typing.Protocol
。
12.5.2
节实现的
__getitem__
(参见示例
12-6
)比第
1
版更简洁且更强健,这要得益于鸭子
类型和
operator.index
。本章后面实现的
Vector
和第
16
章也做了同样的改动。
306
|
第
12
章
下面开始吧!
12.2
Vector
类
:
用户定义的序列类型
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版)
Posts & Telecom Press, Michał Jaworski, Tarek Ziadé
Python语言及其应用(第2版)
Bill Lubanovic
Kafka权威指南(第2版)
Gwen Shapira, Todd Palino, Rajini Sivaram, Krit Petty
Python贝叶斯分析(第2版)
Posts & Telecom Press, Osvaldo Martin
Publisher Resources
ISBN: 9787115612366