Skip to Content
Python语言及其应用(第2版)
book

Python语言及其应用(第2版)

by Bill Lubanovic
March 2022
Intermediate to advanced
522 pages
13h 52m
Chinese
Posts & Telecom Press
Content preview from Python语言及其应用(第2版)
数据:类型、值、变量和名称
27
在这个例子中,已经用不着值为
5
12
7
的对象以及变量
x
y
了。
Python
的垃圾回收
器可以选择将其销毁或是送到错位物品岛(
Island of Misfit Objects
4
,也可以考虑到性能原
因,将其保留一段时间,因为小整数往往会被大量使用。
2.8
向多个名称赋值
可以同时向多个变量名称赋值。
>>> two = deux = zwei = 2
>>> two
2
>>> deux
2
>>> zwei
2
2.9
为名称重新赋值
因为名称指向对象,所以修改名称的值只不过是让名称指向新的对象。旧对象的引用计数
减少了,新对象的引用计数增加了。
2.10
复制
如图
2-4
所示,将已有的变量
a
赋给新变量
b
只是让
b
a
指向同一对象而已。不管你使
a
还是
b
,引用的都是相同的对象。
如果该对象是不可变的(比如整数),则其值无法被修改,所以这两个名称其实都是只读
的。尝试下列代码:
>>> x = 5
>>> x
5
>>> y = x
>>> y
5
>>> x = 29
>>> x
29
>>> y
5
x
赋给
y
时,名称
y
指向的是值为
5
的整数对象,这也是
x
所指向的对象。修改
x
会使
其指向值为
29
的新整数对象。这不会改变
y
仍指向的那个值为
5
的对象。
但如果两者指向的是一个
可变
对象,则可以通过任意名称修改该对象。如果事先不知道这
一点,你肯定会大吃一惊。
4
“错位物品岛”模仿了儿童读物《红鼻驯鹿鲁道夫》(
Rudolph The Red-Nosed Reindeer
)中的“错位玩 ...
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

可编程网络自动化

可编程网络自动化

Jason Edelman, Scott S. Lowe, Matt Oswalt
Python实用技能学习指南

Python实用技能学习指南

Posts & Telecom Press, Robert Smallshire, Austin Bingham
量子计算机编程:从入门到实践

量子计算机编程:从入门到实践

Eric R. Johnston, Nicholas Harrigan, Mercedes Gimeno-Segovia
Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano

Publisher Resources

ISBN: 9787115586223