Skip to Content
《学习 Python》第 5 版
book

《学习 Python》第 5 版

by Mark Lutz
May 2025
Intermediate to advanced
1648 pages
23h 5m
Chinese
O'Reilly Media, Inc.
Content preview from 《学习 Python》第 5 版

第 28 章 一个更现实的例子

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

我们将在下一章深入探讨更多的类语法细节。不过,在此之前,我想先向大家展示一个更现实的类实例,它比我们目前看到的更实用。在本章中,我们将构建一组更具体的类--记录和处理关于人的信息。正如您将看到的,我们在 Python 编程中称之为实例的东西,通常可以起到与传统意义上的记录程序相同的作用。

具体来说,本章我们将编写两个类的代码:

  • Person-创建和处理有关人员信息的类别

  • ManagerPerson 进行定制,修改继承行为

在此过程中,我们将制作这两个类的实例,并测试它们的功能。完成后,我将向你展示一个很好的类用例--我们将把实例存储在面向对象的搁架数据库中,使它们永久存在。这样,你就可以用这段代码作为模板,来充实一个完全用 Python 编写的个人数据库。

除了实际的实用性,我们在这里的目的还有教育性:本章提供了 Python 面向对象编程的教程。通常,人们在纸上掌握了上一章的类语法,但在面对从头开始编写一个新类时,却不知如何下手。为此,我们将一步一步地帮助您学习基础知识;我们将逐步建立类,这样您就可以看到它们的特性是如何组合成完整程序的。

最终,我们的类在代码量上仍然相对较少,但它们将展示 Python OOP 模型的所有主要思想。尽管有语法上的细节,Python 的类系统在很大程度上其实只是在对象树中搜索一个属性,以及函数的一个特殊的第一参数。

步骤 1:创建实例

好了,设计阶段就到此为止,让我们进入实施阶段。我们的首要任务是开始编写主类Person 。在您最喜欢的文本编辑器中,为我们要编写的代码打开一个新文件。在 Python 中,模块名以小写字母开头,类名以大写字母开头是一个相当强的惯例;就像self 方法中的参数名一样,这并不是语言所要求的,但它是如此普遍,以至于偏离它可能会让后来阅读您代码的人感到困惑。为了符合要求,我们将把新模块文件person.py和其中的类称为Person ,就像这样:

# File person.py (start)

class Person:                             # Start a class

我们所有的工作都将在这个文件中完成,直到本章的后面部分。 在 Python 中,我们可以在一个模块文件中编写任意数量的函数和类,如果我们以后在其中添加无关的组件,这个文件的person.py名称可能就没有什么意义了。现在,我们假定其中的所有内容都与Person 相关。无论如何都应该是这样--因为我们已经了解到,当模块有一个统一目的时,它们往往能发挥最大的作用。

编码构造器

现在,我们 Person 类要做的第一件事就是记录人们的基本信息--填写记录字段。当然,这些在 Python 术语中被称为实例对象属性,它们通常是通过在类的方法函数中赋值给self 属性来创建的。给实例属性赋予第一个值的正常方法是在 __init__构造函数方法中赋值给self ,该方法包含 Python 在每次创建实例时自动运行的代码。让我们为类添加一个构造方法:

# Add record field initialization

class Person:
    def __init__(self, name, job, pay):      # Constructor takes three arguments
        self.name = name                     # Fill out fields when created
        self.job  = job                      # self is ...
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

学习 Java,第 6 版

学习 Java,第 6 版

Marc Loy, Patrick Niemeyer, Daniel Leuck
《高性能 Python》第二版

《高性能 Python》第二版

Micha Gorelick, Ian Ozsvald

Publisher Resources

ISBN: 9798341656963