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 技術手冊 第三版
Cython
|
765
Cython 運算式
除了 Python 運算式語法,Cython 可以使用一些(但非全部的)C 語法。
要取得變數
var
的位址,就使用
&var
,就像 C 中那樣。然而,要解參考
dereference)一個指標
p
,要用
p[0]
,等效的 C 語法
*p
Cython 中無
效。在 C 中你會使用
p->q
的地方,在 Cython 中就使用
p.q
Null 指標使
Cython 的關鍵字
NULL
。字元常數(char constants)使用語法
c'x'
。轉型
casts)使用角括號(angle brackets),例如 C 中的
(int)somefloat
要寫
<int>somefloat
,此外,只在 C 值上使用轉型,轉成 C 型別,
永不
用在
Python 的值與型別上(遇到 Python 值或型別時,讓 Cython 為你自動進行
型別轉換)。
一個 Cython 範例:最大公因數
Euclid(歐幾里得)用於兩個數字的 GCDGreatest Common Divisor,最
大公因數)演算法以純 Python 實作起來相當簡單:
def gcd(dividend, divisor):
remainder = dividend % divisor
while remainder:
dividend = divisor
divisor = remainder
remainder = dividend % divisor
return divisor
Cython
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