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 技術手冊 第三版
256
|
7 章 核心內建功能和標準程式庫模組
L = map(functools.partial(max, 0), L)
可用來取代這個用到 lambda 的程式碼片段:
L = map(lambda x: max(0, x), L)
以及最簡潔的做法,使用一個串列概括式:
L = [max(0, x) for x in L]
functools.partial
特別適合用於需要 callbacks(回呼)的情況,例如
GUI 的事件驅動程式設計(event-driven programming)與網路應用
程式(涵蓋於第 18 )。
partial
回傳的 callable 帶有屬性
func
(所包裹的函式)、
args
(預先
繫結的位置引數所成的
tuple
),以及
keywords
(預先繫結的具名引數
所成的
dict
,或為
None
)。
reduce reduce(func,seq[,init])
就像內建函式
reduce
,涵蓋於表 7-2。在 v3 中,
reduce
不是內建的,
但仍然可在
functools
中取用。
total_ordering
一個類別裝飾器,適合用來裝飾至少提供一個不等性比較方法
inequality comparison method,例如
__lt_ _
)並也提供
__eq_ _
類別。基於類別現有的方法,類別裝飾器
total_order
會為類別新增其
他所有的不等性比較方法,讓你不必為它們加入公式化的程式碼。
wraps wraps(wrapped)
一個裝飾器,適合用來裝飾包裹(
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