Skip to Content
Python 技術手冊 第三版
book

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate to advanced
856 pages
17h 17m
Chinese
GoTop Information, Inc.
Content preview from Python 技術手冊 第三版
運算式和運算子
|
67
Python
del 述句
儘管其名稱是那樣,但一個
del
述句做的是
解除參考的繫結
unbinds
references
,而
,就其本身而言,刪除物件。當繫結的解除使得一個物
件不再有指向它的參考,物件的刪除就會像隨之而來的後果一樣,由記憶
體回收(garbage collection)機制自動進行。
一個
del
述句的組成是關鍵字
del
後面接著一或多個由逗號(
,
)分隔的目
標參考。每個目標都可以是一個變數、屬性參考、索引動作或切片動作,
就跟指定述句一樣,而且
del
執行的時候必須已經有繫結。當
del
的目標
是一個識別字,
del
述句就會解除那個變數的繫結。如果識別字已經繫結,
那麼解除繫結的動作永遠都不會被禁止,只要請求,就會發生。
在所有其他的情況中,
del
述句是向一個物件發出請求,要求它解除它一或
多個屬性或項目的繫結。一個物件可以拒絕解除某些(或全部)屬性或特
性的繫結,並在你嘗試進行不允許的解除繫結動作時,提出一個例外(也
請參閱後面「一般用途的特殊方法」中的
_ _delattr_ _
和「容器方法」中
_ _delitem_ _
)。對一個切片動作解除繫結,效果通常與指定一個空序列
給那個切片動作相同,但要不要這樣實作,是由容器方法來決定。
容器也能讓
del
具有副作用。舉例來說,假設
del
C[2]
成功了,其中
C
一個字典,這會使得未來對
C[2]
的參考都無效(提出
KeyError
例外),除
非你再次指定值給
C[2]
。但如果
C
是一個串列,
del
C[2]
會使得接在
C
面的每個項目都 ...
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

利用 Python 进行数据分析(原书第2版)

利用 Python 进行数据分析(原书第2版)

Wes McKinney

Publisher Resources

ISBN: 9789864766819