Skip to Content
Python实用技能学习指南
book

Python实用技能学习指南

by Posts & Telecom Press, Robert Smallshire, Austin Bingham
February 2024
Intermediate to advanced
316 pages
3h 47m
Chinese
Packt Publishing
Content preview from Python实用技能学习指南

第4章 内置类型和对象模型

Python语言的最基本的设计元素之一是对象。对象是主要的数据结构,它不仅是用户级别的语法结构,而且也是语言本身的内部工作机制。在本章中,我们将从原理和实践两个角度来讲解这些知识,希望你能领会Python中无处不在的对象。

我们将学习什么是对象,如何使用对象,以及如何管理对象的引用。我们还将开始探索Python中类型的概念,并且讲解Python的类型与许多其他流行语言的类型之间的相同点和不同点。在本章,我们还将更深入地了解已经用到的一些集合类型,并介绍一些其他的集合类型。

在之前的章节中,我们已经讨论并使用了Python中的“变量”,那变量究竟是什么?将一个整数赋值给一个变量:

>>> x = 1000

当我们这样做时,到底发生了什么呢?首先,Python创建了一个值为1000的int对象。该对象是匿名的,它的本身并不具有名称(x或其他)。它只是一个Python运行时由系统分配和跟踪的对象。

创建对象后,Python创建了一个名为x的对象引用(object reference),并安排x指向int(1000)对象,如图4.1所示。

..\tu\0401.tif

图4.1 将值为1000的整数对象赋值给x

现在我们将通过重赋值来修改x的值:

>>> x = 500

这并不会改变我们之前构造的int(1000)对象。Python中的整数对象是不可变的,也无法被更改。实际上,过程是这样的:Python首先创建一个值为500的新的不可变整数对象,然后将 ...

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

人工智能技术与大数据

人工智能技术与大数据

Posts & Telecom Press, Anand Deshpande, Manish Kumar
Kafka权威指南(第2版)

Kafka权威指南(第2版)

Gwen Shapira, Todd Palino, Rajini Sivaram, Krit Petty

Publisher Resources

ISBN: 9781835889626