復(fù)數(shù)對(duì)象?
從C API看,Python的復(fù)數(shù)對(duì)象由兩個(gè)不同的部分實(shí)現(xiàn):一個(gè)是在Python程序使用的Python對(duì)象,另外的是一個(gè)代表真正復(fù)數(shù)值的C結(jié)構(gòu)體。API提供了函數(shù)共同操作兩者。
表示復(fù)數(shù)的C結(jié)構(gòu)體?
需要注意的是接受這些結(jié)構(gòu)體的作為參數(shù)并當(dāng)做結(jié)果返回的函數(shù),都是傳遞“值”而不是引用指針。此規(guī)則適用于整個(gè)API。
-
type Py_complex?
這是一個(gè)對(duì)應(yīng)Python復(fù)數(shù)對(duì)象的值部分的C結(jié)構(gòu)體。絕大部分處理復(fù)數(shù)對(duì)象的函數(shù)都用這類型的結(jié)構(gòu)體作為輸入或者輸出值,它可近似地定義為:
typedef struct { double real; double imag; } Py_complex;
-
Py_complex _Py_c_sum(Py_complex left, Py_complex right)?
返回兩個(gè)復(fù)數(shù)的和,用 C 類型
Py_complex
表示。
-
Py_complex _Py_c_diff(Py_complex left, Py_complex right)?
返回兩個(gè)復(fù)數(shù)的差,用 C 類型
Py_complex
表示。
-
Py_complex _Py_c_neg(Py_complex num)?
返回復(fù)數(shù) num 的負(fù)值,用 C
Py_complex
表示。
-
Py_complex _Py_c_prod(Py_complex left, Py_complex right)?
返回兩個(gè)復(fù)數(shù)的乘積,用 C 類型
Py_complex
表示。
-
Py_complex _Py_c_quot(Py_complex dividend, Py_complex divisor)?
返回兩個(gè)復(fù)數(shù)的商,用 C 類型
Py_complex
表示。如果 divisor 為空,這個(gè)方法返回零并設(shè)置
errno
為EDOM
。
-
Py_complex _Py_c_pow(Py_complex num, Py_complex exp)?
返回 num 的 exp 次冪,用 C 類型
Py_complex
表示。如果 num 為空且 exp 不是正實(shí)數(shù),這個(gè)方法返回零并設(shè)置
errno
為EDOM
。
表示復(fù)數(shù)的Python對(duì)象?
-
PyTypeObject PyComplex_Type?
- Part of the Stable ABI.
這是個(gè)屬于
PyTypeObject
的代表Python復(fù)數(shù)類型的實(shí)例。在Python層面的類型complex
是同一個(gè)對(duì)象。
-
int PyComplex_Check(PyObject *p)?
如果它的參數(shù)是一個(gè)
PyComplexObject
或者PyComplexObject
的子類型則返回真值。 此函數(shù)總是會(huì)成功執(zhí)行。
-
int PyComplex_CheckExact(PyObject *p)?
如果它的參數(shù)是一個(gè)
PyComplexObject
但不是PyComplexObject
的子類型則返回真值。 此函數(shù)總是會(huì)成功執(zhí)行。
-
PyObject *PyComplex_FromCComplex(Py_complex v)?
- Return value: New reference.
根據(jù)C類型
Py_complex
的值生成一個(gè)新的Python復(fù)數(shù)對(duì)象。
-
PyObject *PyComplex_FromDoubles(double real, double imag)?
- Return value: New reference. Part of the Stable ABI.
根據(jù) real 和 imag 返回一個(gè)新的C類型
PyComplexObject
對(duì)象。
-
double PyComplex_RealAsDouble(PyObject *op)?
- Part of the Stable ABI.
以C類型 double 返回 op 的實(shí)部。
-
double PyComplex_ImagAsDouble(PyObject *op)?
- Part of the Stable ABI.
以C類型 double 返回 op 的虛部。
-
Py_complex PyComplex_AsCComplex(PyObject *op)?
返回復(fù)數(shù) op 的C類型
Py_complex
值。如果 op 不是一個(gè) Python 復(fù)數(shù)對(duì)象,但是具有
__complex__()
方法,此方法將首先被調(diào)用,將 op 轉(zhuǎn)換為一個(gè) Python 復(fù)數(shù)對(duì)象。 如果__complex__()
未定義則將回退至__float__()
,如果__float__()
未定義則將回退至__index__()
。 如果失敗,此方法將返回-1.0
作為實(shí)數(shù)值。在 3.8 版更改: 如果可用將使用
__index__()
。