Skip to Content
Python for Excel (Chinese Edition), 2nd Edition
book

Python for Excel (Chinese Edition), 2nd Edition

by Felix Zumstein
May 2026
Intermediate
418 pages
5h 50m
Chinese
O'Reilly Media, Inc.
Content preview from Python for Excel (Chinese Edition), 2nd Edition

附录 C. Python高级 概念

在本附录中,我们将深入探讨以下三个主题:类与对象、支持时区的 datetime 对象,以及可变对象与不可变对象。这些主题彼此独立,因此您可以按任意顺序阅读。

类与对象

在本节中,我们将编写自己的类,以更好地理解类与对象之间的关系。类定义了新的对象类型:类就像你用来制造汽车的蓝图。根据你使用的规格,你会得到不同的汽车——例如,一辆蓝色汽车或一辆红色汽车。 从蓝图(类)中生成汽车(对象)的过程称为实例化,这也是为什么对象也被称为类实例。类允许你定义新的数据类型,将相关 的数据(属性)和函数(方法)整合在一起,从而帮助你构建和组织代码。 现在,让我们使用数据类来定义一个Car类。你可以通过@dataclass装饰器将普通类转换为 数据类。数据类通过处理使用普通类时必须编写的冗余代码,使编写类变得更加容易:

In [1]: from dataclasses import dataclass
In [2]: @dataclass
        class Car:
            # Data classes require type hints for attributes
            color: str
            speed: int = 0

            def accelerate(self, speed_increase):
                self.speed += speed_increase

这是一个简单的 Car 类,包含两个属性(colorspeed)和一个方法(accelerate)。方法是作为类定义一部分的函数。 accelerate 方法将修改该类实例的数据(speed)。首先,让我们实例化两辆汽车。操作方式与调用函数相同:通过添加圆括号并提供与数据类中定义的属性相对应的参数来调用类:

In [3]: # Let's instantiate two car objects
        car1 = Car("red")
        car2 = Car(color="blue")

关于函数参数的所有规则在此同样适用:对于car1我们提供位置参数;而对于car2我们使用关键字参数。从Car类实例化这两个汽车对象后,我们将查看它们的属性并调用它们的方法。正如我们将看到的,加速car1 后,car1的速度发生了变化,但car2的速度保持不变,因为这两个对象彼此独立:

In [4]: # Attributes give you access to the data of an object
        car1.color
Out[4]: 'red'
In [5]: car1.speed
Out[5]: 0
In [6]: # Calling the accelerate method on car1
        car1.accelerate(20)
In [7]: # The speed attribute of car1 changed
        car1.speed
Out[7]: 20
In [8]: # The speed attribute of car2 remained the same
        car2.speed
Out[8]: 0

Python 还允许你直接修改属性,而无需使用方法:

In [9]: car1.color = "green"
In [10]: car1.color
Out[10]: 'green'
In [11]: car2.color  # unchanged
Out[11]: 'blue'

总结:类定义了对象的属性和方法。类允许你将相关的函数(“方法”)和数据(“属性”)分组,以便 可以通过点表示法( ...

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

学习勒索软件响应和恢复 (Chinese Edition)

学习勒索软件响应和恢复 (Chinese Edition)

W. Curtis Preston, Michael Saylor
Prometheus:快速入门,第二版

Prometheus:快速入门,第二版

Julien Pivotto, Brian Brazil

Publisher Resources

ISBN: 0642572396008