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
Bill Lubanovic
March 2022
Intermediate to advanced
522 pages
13h 52m
Chinese
Posts & Telecom Press
Content preview from
Python语言及其应用(第2版)
对象和类
|
169
10.13 attrs
你已经知道了如何创建类和添加特性,以及其中涉及的大量其他操作,比如定义
__init__()
,
将参数赋给
self
,创建
__str__()
等双下划线方法。具名元组和数据类是标准库中的替代
方案,当主要想创建数据集合时,使用它们可能会更简单。
“
The
One Python Library Everyone Needs
”一文中比较了普通类、具名元组以及数据类。该
文基于多方面的原因(比如减少键盘输入、数据验证等)推荐了第三方包
attrs
。
不妨了解
一下,看看是否比内建解决方案更合你心意。
10.14
后续内容
在第
13
章中,我们将在代码结构中更进一步,学习
Python
模块
和
包
。
10.15
练习
1.
创建一个名为
Thing
的空类并将其打印出来。然后,生成一个属于该类的对象
example
并打印。两次的打印结果是否相同?
2.
创建新类
Thing2
,将
'abc'
赋给类特性
letters
。打印出
letters
。
3.
再
创建另一个类
Thing3
。这次将
'xyz'
赋给实例(对象)特性
letters
。打印出
letters
。是否需要为此生成该类的对象?
4.
创建包含特性
name
、
symbol
和
number
的类
Element
。生成该类的对象,特性值分别为
'Hydrogen'
、
'H'
和
1
。
5.
创建一个字典,键和值为:
'name': 'Hydrogen'
、
'symbol': 'H'
和
'number': 1
。然后,
使用该字典生成
Element
类的对象
hydrogen
。
6.
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编程入门与实战
Posts & Telecom Press, Fabrizio Romano
Python实用技能学习指南
Posts & Telecom Press, Robert Smallshire, Austin Bingham
Python技术基础视频教程
保罗·J·戴特尔
Python面向对象编程指南
Posts & Telecom Press, Steven F. Lott
Publisher Resources
ISBN: 9787115586223