numbers
--- 數值的抽象基底類別¶
原始碼:Lib/numbers.py
numbers
模組 (PEP 3141) 定義了數值抽象基底類別 的階層結構,其中逐一定義了更多操作。此模組中定義的型別都不可被實例化。
- classnumbers.Number¶
數值階層結構的基礎。如果你只想確認引數x 是不是數值、並不關心其型別,請使用
isinstance(x,Number)
。
數值的階層¶
- classnumbers.Complex¶
這個型別的子類別描述了複數並包含適用於內建
complex
型別的操作。這些操作有:complex
和bool
的轉換、real
、imag
、+
、-
、*
、/
、**
、abs()
、conjugate()
、==
以及!=
。除-
和!=
之外所有操作都是抽象的。- real¶
為抽象的。取得該數值的實數部分。
- imag¶
為抽象的。取得該數值的虛數部分。
- abstractmethodconjugate()¶
為抽象的。回傳共軛複數,例如
(1+3j).conjugate()==(1-3j)
。
- classnumbers.Real¶
相對於
Complex
,Real
加入了只有實數才能進行的操作。簡單的說,有
float
的轉換、math.trunc()
、round()
、math.floor()
、math.ceil()
、divmod()
、//
、%
、<
、<=
、>
、和>=
。實數同樣提供
complex()
、real
、imag
和conjugate()
的預設值。
- classnumbers.Rational¶
Real
的子型別,並增加了numerator
和denominator
這兩種特性。它也會提供float()
的預設值。numerator
和denominator
的值必須是Integral
的實例且denominator
要是正數。- numerator¶
為抽象的。
- denominator¶
為抽象的。
給型別實作者的註記¶
實作者需注意,相等的數值除了大小相等外,還必須擁有同樣的雜湊值。當使用兩個不同的實數擴充時,這可能是很微妙的。例如,fractions.Fraction
底下的hash()
實作如下:
def__hash__(self):ifself.denominator==1:# 正確處理整數。returnhash(self.numerator)# 檢查費時,但絕對正確。ifself==float(self):returnhash(float(self))else:# 使用 tuple 的雜湊值以避免簡單分數上的高碰撞率。returnhash((self.numerator,self.denominator))
加入更多數值 ABC¶
當然,還有更多用於數值的 ABC,如果不加入它們就不會有健全的階層。你可以在Complex
和Real
中加入MyFoo
,像是:
classMyFoo(Complex):...MyFoo.register(Real)
實作算術操作¶
我們想要實作算術操作,來使得混合模式操作要麼呼叫一個作者知道兩個引數之型別的實作,要麼將其轉換成最接近的內建型別並執行這個操作。對於Integral
的子型別,這意味著__add__()
和__radd__()
必須用如下方式定義:
classMyIntegral(Integral):def__add__(self,other):ifisinstance(other,MyIntegral):returndo_my_adding_stuff(self,other)elifisinstance(other,OtherTypeIKnowAbout):returndo_my_other_adding_stuff(self,other)else:returnNotImplementeddef__radd__(self,other):ifisinstance(other,MyIntegral):returndo_my_adding_stuff(other,self)elifisinstance(other,OtherTypeIKnowAbout):returndo_my_other_adding_stuff(other,self)elifisinstance(other,Integral):returnint(other)+int(self)elifisinstance(other,Real):returnfloat(other)+float(self)elifisinstance(other,Complex):returncomplex(other)+complex(self)else:returnNotImplemented
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):defforward(a,b):ifisinstance(b,(int,Fraction)):returnmonomorphic_operator(a,b)elifisinstance(b,float):returnfallback_operator(float(a),b)elifisinstance(b,complex):returnfallback_operator(complex(a),b)else:returnNotImplementedforward.__name__='__'+fallback_operator.__name__+'__'forward.__doc__=monomorphic_operator.__doc__defreverse(b,a):ifisinstance(a,Rational):# 包含整數。returnmonomorphic_operator(a,b)elifisinstance(a,Real):returnfallback_operator(float(a),float(b))elifisinstance(a,Complex):returnfallback_operator(complex(a),complex(b))else:returnNotImplementedreverse.__name__='__r'+fallback_operator.__name__+'__'reverse.__doc__=monomorphic_operator.__doc__returnforward,reversedef_add(a,b):"""a + b"""returnFraction(a.numerator*b.denominator+b.numerator*a.denominator,a.denominator*b.denominator)__add__,__radd__=_operator_fallbacks(_add,operator.add)# ...