numbers --- 數值的抽象基底類別¶
原始碼:Lib/numbers.py
numbers 模块 (PEP 3141) 定义了数字 抽象基类 的层级结构,其中逐级定义了更多的操作。 此模块中定义的类型都不可被实例化。
- class numbers.Number¶
- 數值階層結構的基礎。如果你只想確認引數 x 是不是數值、並不關心其型別,請使用 - isinstance(x, Number)。
數值的階層¶
- class numbers.Complex¶
- 這個型別的子類別描述了複數並包含適用於內建 - complex型別的操作。這些操作有:- complex和- bool的轉換、- real、- imag、- +、- -、- *、- /、- **、- abs()、- conjugate()、- ==以及- !=。除- -和- !=之外所有操作都是抽象的。- real¶
- 為抽象的。取得該數值的實數部分。 
 - imag¶
- 為抽象的。取得該數值的虛數部分。 
 - abstractmethod conjugate()¶
- 為抽象的。回傳共軛複數,例如 - (1+3j).conjugate() == (1-3j)。
 
- class numbers.Real¶
- 相对于 - Complex,- Real加入了只适用于实数的操作。- 簡單的說,有 - float的轉換、- math.trunc()、- round()、- math.floor()、- math.ceil()、- divmod()、- //、- %、- <、- <=、- >、和- >=。- 實數同樣提供 - complex()、- real、- imag和- conjugate()的預設值。
- class numbers.Rational¶
- Real的子型別,並增加了- numerator和- denominator這兩種特性。它也會提供- float()的預設值。- numerator和- denominator的值必須是- Integral的實例且- denominator要是正數。- numerator¶
- 為抽象的。 
 - denominator¶
- 為抽象的。 
 
給型別實作者的註記¶
實作者需注意,相等的數值除了大小相等外,還必須擁有同樣的雜湊值。當使用兩個不同的實數擴充時,這可能是很微妙的。例如,fractions.Fraction 底下的 hash() 實作如下:
def __hash__(self):
    if self.denominator == 1:
        # Get integers right.
        return hash(self.numerator)
    # Expensive check, but definitely correct.
    if self == float(self):
        return hash(float(self))
    else:
        # Use tuple's hash to avoid a high collision rate on
        # simple fractions.
        return hash((self.numerator, self.denominator))
加入更多數值 ABC¶
當然,還有更多用於數值的 ABC,如果不加入它們就不會有健全的階層。你可以在 Complex 和 Real 中加入 MyFoo,像是:
class MyFoo(Complex): ...
MyFoo.register(Real)
實作算術操作¶
我们想要实现算术运算,因此混合模式运算要么调用一个开发者知道两个参数类型的实现,要么将两个参数转换为最接近的内置类型再执行运算。 对于 Integral 的子类,这意味着 __add__() 和 __radd__() 应当被定义为:
class MyIntegral(Integral):
    def __add__(self, other):
        if isinstance(other, MyIntegral):
            return do_my_adding_stuff(self, other)
        elif isinstance(other, OtherTypeIKnowAbout):
            return do_my_other_adding_stuff(self, other)
        else:
            return NotImplemented
    def __radd__(self, other):
        if isinstance(other, MyIntegral):
            return do_my_adding_stuff(other, self)
        elif isinstance(other, OtherTypeIKnowAbout):
            return do_my_other_adding_stuff(other, self)
        elif isinstance(other, Integral):
            return int(other) + int(self)
        elif isinstance(other, Real):
            return float(other) + float(self)
        elif isinstance(other, Complex):
            return complex(other) + complex(self)
        else:
            return NotImplemented
Complex 的子類別有 5 種不同的混合型別操作。我將上面提到所有不涉及 MyIntegral 和 OtherTypeIKnowAbout 的程式碼稱作「模板 (boilerplate)」。a 是 Complex 之子型別 A 的實例 (a : A <: Complex),同時 b : B <: Complex。我將要計算 a + b:
- 如果 - A定义了接受- b的- __add__(),一切都没有问题。
- 如果 - A回退至基础代码,它将返回一个来自- __add__()的值,我们就没有机会为- B定义更加智能的- __radd__(),因此基础代码应当从- __add__()返回- NotImplemented。 (或者- A可能完全不实现- __add__()。)
- 那么 - B的- __radd__()将有机会发挥作用。 如果它接受- a,一切都没有问题。
- 如果沒有成功回退到模板,就沒有更多的方法可以去嘗試,因此這裡將使用預設的實作。 
- 如果 - B <: A,Python 會在- A.__add__之前嘗試- B.__radd__。這是可行的,因為它是透過對- A的理解而實作的,所以這可以在交給- Complex之前處理好這些實例。
如果 A <: Complex 和 B <: Real 没有共享任何其他信息,那么内置 complex 的共享操作就是最适当的,两个 __radd__() 都将应用该操作,因此 a+b == b+a。
由於大部分對任意給定類型的操作都十分相似的,定義一個為任意給定運算子生成向前 (forward) 與向後 (reverse) 實例的輔助函式可能會非常有用。例如,fractions.Fraction 使用了:
def _operator_fallbacks(monomorphic_operator, fallback_operator):
    def forward(a, b):
        if isinstance(b, (int, Fraction)):
            return monomorphic_operator(a, b)
        elif isinstance(b, float):
            return fallback_operator(float(a), b)
        elif isinstance(b, complex):
            return fallback_operator(complex(a), b)
        else:
            return NotImplemented
    forward.__name__ = '__' + fallback_operator.__name__ + '__'
    forward.__doc__ = monomorphic_operator.__doc__
    def reverse(b, a):
        if isinstance(a, Rational):
            # Includes ints.
            return monomorphic_operator(a, b)
        elif isinstance(a, Real):
            return fallback_operator(float(a), float(b))
        elif isinstance(a, Complex):
            return fallback_operator(complex(a), complex(b))
        else:
            return NotImplemented
    reverse.__name__ = '__r' + fallback_operator.__name__ + '__'
    reverse.__doc__ = monomorphic_operator.__doc__
    return forward, reverse
def _add(a, b):
    """a + b"""
    return Fraction(a.numerator * b.denominator +
                    b.numerator * a.denominator,
                    a.denominator * b.denominator)
__add__, __radd__ = _operator_fallbacks(_add, operator.add)
# ...