numbers
--- 數(shù)字的抽象基類(lèi)?
源代碼: Lib/numbers.py
numbers
模塊 (PEP 3141) 定義了數(shù)字 抽象基類(lèi) 的層級(jí)結(jié)構(gòu),其中逐級(jí)定義了更多操作。 此模塊中定義的類(lèi)型都不可被實(shí)例化。
- class numbers.Number?
數(shù)字的層次結(jié)構(gòu)的基礎(chǔ)。 如果你只想確認(rèn)參數(shù) x 是不是數(shù)字而不關(guān)心其類(lèi)型,則使用
isinstance(x, Number)
。
數(shù)字的層次?
- class numbers.Complex?
這個(gè)類(lèi)型的子類(lèi)描述了復(fù)數(shù)并包括了適用于內(nèi)置
complex
類(lèi)型的操作。 這些操作有: 轉(zhuǎn)換為complex
和bool
,real
,imag
,+
,-
,*
,/
,**
,abs()
,conjugate()
,==
以及!=
。 除-
和!=
之外所有操作都是抽象的。- real?
抽象的。得到該數(shù)字的實(shí)數(shù)部分。
- imag?
抽象的。得到該數(shù)字的虛數(shù)部分。
- abstractmethod conjugate()?
抽象的。返回共軛復(fù)數(shù)。例如
(1+3j).conjugate() == (1-3j)
。
- class numbers.Real?
相對(duì)于
Complex
,Real
加入了只有實(shí)數(shù)才能進(jìn)行的操作。簡(jiǎn)單的說(shuō),它們是:轉(zhuǎn)化至
float
,math.trunc()
、round()
、math.floor()
、math.ceil()
、divmod()
、//
、%
、<
、<=
、>
、 和>=
。實(shí)數(shù)同樣默認(rèn)支持
complex()
、real
、imag
和conjugate()
。
類(lèi)型接口注釋。?
實(shí)現(xiàn)者需要注意使相等的數(shù)字相等并擁有同樣的值。當(dāng)這兩個(gè)數(shù)使用不同的擴(kuò)展模塊時(shí),這其中的差異是很微妙的。例如,用 fractions.Fraction
實(shí)現(xiàn) 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))
加入更多數(shù)字的ABC?
當(dāng)然,這里有更多支持?jǐn)?shù)字的ABC,如果不加入這些,就將缺少層次感。你可以用如下方法在 Complex
和 Real
中加入 MyFoo
:
class MyFoo(Complex): ...
MyFoo.register(Real)
實(shí)現(xiàn)算術(shù)運(yùn)算?
我們希望實(shí)現(xiàn)計(jì)算,因此,混合模式操作要么調(diào)用一個(gè)作者知道參數(shù)類(lèi)型的實(shí)現(xiàn),要么轉(zhuǎn)變成為最接近的內(nèi)置類(lèi)型并對(duì)這個(gè)執(zhí)行操作。對(duì)于子類(lèi) 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 種不同的混合類(lèi)型的操作。 我將上面提到的所有代碼作為“模板”稱作 MyIntegral
和 OtherTypeIKnowAbout
。 a
是 Complex
的子類(lèi)型 A
的實(shí)例 (a : A <: Complex
),同時(shí) b : B <: Complex
。 我將要計(jì)算 a + b
:
如果
A
被定義成一個(gè)承認(rèn)b
的__add__()
,一切都沒(méi)有問(wèn)題。如果
A
轉(zhuǎn)回成“模板”失敗,它將返回一個(gè)屬于__add__()
的值,我們需要避免B
定義了一個(gè)更加智能的__radd__()
,因此模板需要返回一個(gè)屬于__add__()
的NotImplemented
。(或者A
可能完全不實(shí)現(xiàn)__add__()
。)接著看
B
的__radd__()
。如果它承認(rèn)a
,一切都沒(méi)有問(wèn)題。如果沒(méi)有成功回退到模板,就沒(méi)有更多的方法可以去嘗試,因此這里將使用默認(rèn)的實(shí)現(xiàn)。
如果
B <: A
, Python 在A.__add__
之前嘗試B.__radd__
。 這是可行的,是通過(guò)對(duì)A
的認(rèn)識(shí)實(shí)現(xiàn)的,因此這可以在交給Complex
處理之前處理這些實(shí)例。
如果 A <: Complex
和 B <: Real
沒(méi)有共享任何資源,那么適當(dāng)?shù)墓蚕聿僮魃婕皟?nèi)置的 complex
,并且分別獲得 __radd__()
,因此 a+b == b+a
。
由于對(duì)任何一直類(lèi)型的大部分操作是十分相似的,可以定義一個(gè)幫助函數(shù),即一個(gè)生成后續(xù)或相反的實(shí)例的生成器。例如,使用 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)
# ...