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 版

第 29 章 类编码详情 类编码详情

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

如果您还没有完全理解 Python OOP,不用担心;现在我们已经有了初步的了解,我们将再深入一点,进一步详细研究前面介绍的概念。在本章和下一章中,我们将再次学习类机制。在这里,我们将研究类、方法和继承,将第 27 章中介绍的一些编码思想正式化并加以扩展。因为类是我们最后一个命名空间工具,所以我们也将总结 Python 的命名空间和作用域概念。

下一章将继续深入研究类机制的第二个方面:操作符重载。除了介绍更多细节外,本章和下一章还为我们提供了一个机会,让我们探索一些比迄今为止研究过的类更大的类。

内容说明:如果你一直在线性阅读,本章的部分内容将是对上一章案例研究中介绍的主题的回顾和总结,这里将通过语言主题和更小、更自足的示例来重温这些主题,以方便初学 OOP 的读者。其他人可能会跳过本章的一些内容,但一定要看这里的命名空间覆盖,因为它解释了 Python 类模型中的一些微妙之处。

班级声明

虽然从表面上看,Python 的 class 语句似乎与其它 OOP 语言中的工具相似,但仔细观察,它与一些程序员习惯的工具有很大不同。例如,与 C++ 一样,class语句是 Python 的主要 OOP 工具,但与C++ 不同,Python 的class 不是一个声明。与def 一样,class 语句是一个对象构造器,也是一个隐式赋值--运行时,它会生成一个类对象,并将对它的引用存储在头文件中使用的名称中。也像def 一样,一个class 语句是真正的可执行代码--在 Python 到达并运行定义它的class 语句之前,类并不存在。这通常发生在导入它所编码的模块时,但不会发生在导入之前。

通用表格

class 是一个复合 语句,通常缩进的语句主体出现在头部之下。在标题中,超类列在类名后面的括号中,用逗号隔开。列出多个超类会导致多重继承,我们将在第 31 章对此进行更正式的讨论。下面是语句的一般形式:

class name(superclass,...):           # Assign to name
    attr = value                      # Shared class data
    def method(self,...):             # Methods
        self.attr = value             # Per-instance data

class 语句中,任何赋值都会生成类属性,而特殊命名的方法则会重载操作符;例如,如果定义了名为__init__ 的函数,则会在实例对象构造时调用该函数。

示例

正如我们所见, 类大多只是命名空间,即定义向客户端输出数据和逻辑的名称(即属性)的工具。class 语句实际上定义了一个命名空间。就像在模块文件中一样,嵌套在class 语句正文中的语句创建了它的属性。当 Python 执行一条class语句(不是对类的调用)时,它会从上到下运行其正文中的所有语句。在此过程中发生的赋值会在类的局部作用域中创建名称,这些名称会成为相关类对象中的属性。因此,类 既像模块也像函数

  • 与函数一样,class语句也是本地作用域,嵌套赋值创建的名称就在其中。

  • 与模块中的名称一样,在class 语句中分配的名称也会成为类对象中的属性。

类的主要区别在于它们的命名空间也是 Python继承的基础;在类或实例对象中找不到的引用属性会从其他类中获取。

由于class 是一个复合语句,因此可以在其主体中嵌套任何类型的语句--print 、赋值、ifdef 等等。class ...

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