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 技術手冊 第三版
functools 模組
|
255
_replace p._replace(**kwargs)
回傳
p
的一份拷貝,但其中的零或多個項目以具名引數取代。
有關
namedtuple
用法的更多細節與建議,請參閱線上文件(
http://bit.
ly/2nBbGfK
)。
functools 模組
functools
模組提供了函式與型別來支援 Python 中的函式型程式設計
functional programming),列於表 7-4
7-4
cmp_to_key cmp_to_key(func)
func
必須是具有兩個引數並回傳一個數字的 callable:如果第一個引
數被視為「小於」第二個引數,就回傳
<0
的數字,反過來的話,就
回傳
>0
的數字,如果兩個引數被視為相等,就回傳
0
(就像在 v2
已棄用,在 v3 中已移除的舊有內建函式
cmp
,以及
sort
sorted
有的
cmp=
具名引數)。
cmp_to_key
會回傳一個可呼叫的
k
,適合用作
sort
sorted
min
max
之類函式與方法的具名引數
key=
。這可以用
來將使用舊式
cmp=
引數的程式轉為使用新式的
key=
引數,這在 v3
是必要的,而在 v2 中也是高度建議的。
lru_cache lru_cache(max_size=128, typed=False)
(僅限於 v3,要在 v2 中使用,就
pip install
Jason Coombs 的後向
移植版(https://pypi.python.org/pypi/backports.functools_lru_cache ...
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