复数对象

从C API看,Python的复数对象由两个不同的部分实现:一个是在Python程序使用的Python对象,另外的是一个代表真正复数值的C结构体。API提供了函数共同操作两者。

表示复数的C结构体

需要注意的是接受这些结构体的作为参数并当做结果返回的函数,都是传递“值”而不是引用指针。此规则适用于整个API。

type Py_complex

The C structure which corresponds to the value portion of a Python complex number object. Most of the functions for dealing with complex number objects use structures of this type as input or output values, as appropriate.

double real
double imag

The structure is defined as:

typedef struct {
    double real;
    double imag;
} Py_complex;
Py_complex _Py_c_sum(Py_complex left, Py_complex right)

返回两个复数的和,用 C 类型 Py_complex 表示。

Py_complex _Py_c_diff(Py_complex left, Py_complex right)

返回两个复数的差,用 C 类型 Py_complex 表示。

Py_complex _Py_c_neg(Py_complex num)

返回复数 num 的负值,用 C Py_complex 表示。

Py_complex _Py_c_prod(Py_complex left, Py_complex right)

返回两个复数的乘积,用 C 类型 Py_complex 表示。

Py_complex _Py_c_quot(Py_complex dividend, Py_complex divisor)

返回两个复数的商,用 C 类型 Py_complex 表示。

如果 divisor 为空,则此方法将返回零并将 errno 设为 EDOM

Py_complex _Py_c_pow(Py_complex num, Py_complex exp)

返回 numexp 次幂,用 C 类型 Py_complex 表示。

如果 num 为空且 exp 不是正实数,则此方法将返回零并将 errno 设为 EDOM

表示复数的Python对象

type PyComplexObject

这个C类型 PyObject 的子类型代表一个 Python 复数对象。

PyTypeObject PyComplex_Type
属于 稳定 ABI.

这是个属于 PyTypeObject 的代表Python复数类型的实例。在Python层面的类型 complex 是同一个对象。

int PyComplex_Check(PyObject *p)

如果它的参数是一个 PyComplexObject 或者 PyComplexObject 的子类型则返回真值。 此函数总是会成功执行。

int PyComplex_CheckExact(PyObject *p)

如果它的参数是一个 PyComplexObject 但不是 PyComplexObject 的子类型则返回真值。 此函数总是会成功执行。

PyObject *PyComplex_FromCComplex(Py_complex v)
返回值:新的引用。

Create a new Python complex number object from a C Py_complex value. Return NULL with an exception set on error.

PyObject *PyComplex_FromDoubles(double real, double imag)
返回值:新的引用。 属于 稳定 ABI.

Return a new PyComplexObject object from real and imag. Return NULL with an exception set on error.

double PyComplex_RealAsDouble(PyObject *op)
属于 稳定 ABI.

以 C 类型 double 返回 op 的实部。

Upon failure, this method returns -1.0 with an exception set, so one should call PyErr_Occurred() to check for errors.

double PyComplex_ImagAsDouble(PyObject *op)
属于 稳定 ABI.

以 C 类型 double 返回 op 的虚部。

Py_complex PyComplex_AsCComplex(PyObject *op)

返回复数 op 的C类型 Py_complex 值。

If op is not a Python complex number object but has a __complex__() method, this method will first be called to convert op to a Python complex number object. If __complex__() is not defined then it falls back to __float__(). If __float__() is not defined then it falls back to __index__().

Upon failure, this method returns Py_complex with real set to -1.0 and with an exception set, so one should call PyErr_Occurred() to check for errors.

在 3.8 版本发生变更: 如果可能将使用 __index__()