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版)
对象和类
|
145
不同于模块,你可以同时拥有多个对象(通常称为
实例
),特性可能各不相同,就像包含
代码的超级数据结构。
10.2
简单对象
下面先从基础的对象类开始,稍后再讨论继承。
10.2.1
使用
class
定义类
要创建一个先前没有的新对象,首先要定义
类
,指明其中包含的内容。
第
2
章中将对象比作了塑料盒子
。
类
就像生产这种盒子的模具。例如,
Python
拥有内建
类,可用于创建字符串对象(比如
'cat'
和
'duck'
)和其他标准数据类型(列表、字典
等)。在
Python
中创建自定义对象
,首先需要使用
class
关键字定义类。来看几个简单的
例子。
假设你想定义若干对象来描述猫咪的相关信息。每个对象描述一只猫科动物。先定义名为
Cat
的类作为模具。在下面的例子中,我们尝试编写该类的多个版本,从最简单的形式逐
步演变到具备一定的实用性。
我们遵循
Python PEP-8
的命名惯例。
先写一个最简单的类,不包含任何内容:
>>> class Cat():
... pass
也可以这样写:
>>> class Cat:
... pass
和函数一样,需要写明
pass
,表示该类是空的。以上定义可以创建出一个最小的对象。
可以像函数那样调用类名,创建该类的对象:
>>> a_cat = Cat()
>>> another_cat = Cat()
上述例子调用
Cat()
创建了
Cat
类的两个对象,我们将其分别赋给
a_cat
和
another_cat
。
但是,因为
Cat
类还没有任何代码,所以生成的对象也做不了什么事情。
好吧,还是能做点儿的。 ...
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