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

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate to advanced content levelIntermediate to advanced
856 pages
17h 17m
Chinese
GoTop Information, Inc.
Content preview from Python 技術手冊 第三版
254
|
7 章 核心內建功能和標準程式庫模組
popleft d
.popleft()
d
移除並回傳第一個(最左邊的)項目。如果
d
是空
的,提出
IndexError
rotate d.rotate(n=1)
d
向右轉動(rotate
n
步(如果
n<0
,就向左轉動)。
namedtuple
namedtuple
是一個工廠函式(factory function),會建置並回傳
tuple
的一
個子類別,其實體的項目可用屬性參考(attribute reference)來存取,也
可以用索引。
namedtuple namedtuple(typename,fieldnames)
typename
是一個字串,它是有效的識別字(以一個字母開頭,後面可以接
著字母、數字與底線,不能是保留字,例如
'class'
),作為
namedtuple
所建置並回傳的新類別之名稱。
fieldnames
是字串所構成的一個序列,它
們是有效的識別字,作為新型別之屬性的名稱,依序列出(為了方便,
fieldnames
也可以是單一個字串,其中的識別字以空格或逗號分隔)。
namedtuple
回傳一個型別:你可以將那個型別繫結至一個名稱,然後用它
來製作以位置或具名引數初始化的不可變實體。在那些實體上呼叫
repr
str
會將它們格式化為具名引數的形式,例如:
point = collections.namedtuple('point', 'x,y,z')
p = point(x=1,y=2,z=3) # 可用具名引數 來建置
x, y, z ...
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.
Start your free trial

You might also like

測試驅動開發|使用 Python

測試驅動開發|使用 Python

Harry J.W. Percival
Linux 内核观测技术BPF

Linux 内核观测技术BPF

David Calavera, Lorenzo Fontana

Publisher Resources

ISBN: 9789864766819