日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯網營銷解決方案
創(chuàng)新互聯Python教程:numbers —- 數字的抽象基類

numbers —- 數字的抽象基類

源代碼: Lib/numbers.py

讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領域值得信任、有價值的長期合作伙伴,公司提供的服務項目有:域名注冊、虛擬空間、營銷軟件、網站建設、江安網站維護、網站推廣。


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

Subtypes Real and adds numerator and denominator properties. It also provides a default for float().

The numerator and denominator values should be instances of Integral and should be in lowest terms with denominator positive.

  • numerator

    抽象的。

  • denominator

    抽象的。

class numbers.Integral

子類型 Rational 還增加了到 int 的轉換操作。 為 float(), numerator 和 denominator 提供了默認支持。 為 pow() 方法增加了求余和按位字符串運算的抽象方法: <<, >>, &, ^, |, ~。

類型接口注釋。

實現者需要注意使相等的數字相等并擁有同樣的值。當這兩個數使用不同的擴展模塊時,這其中的差異是很微妙的。例如,用 fractions.Fraction 實現 hash() 如下:

 
 
 
 
  1. def __hash__(self):
  2. if self.denominator == 1:
  3. # Get integers right.
  4. return hash(self.numerator)
  5. # Expensive check, but definitely correct.
  6. if self == float(self):
  7. return hash(float(self))
  8. else:
  9. # Use tuple's hash to avoid a high collision rate on
  10. # simple fractions.
  11. return hash((self.numerator, self.denominator))

加入更多數字的ABC

當然,這里有更多支持數字的ABC,如果不加入這些,就將缺少層次感。你可以用如下方法在 Complex 和 Real 中加入 MyFoo:

 
 
 
 
  1. class MyFoo(Complex): ...
  2. MyFoo.register(Real)

實現算術運算

我們希望實現計算,因此,混合模式操作要么調用一個作者知道參數類型的實現,要么轉變成為最接近的內置類型并對這個執(zhí)行操作。對于子類 Integral,這意味著 __add__()__radd__() 必須用如下方式定義:

 
 
 
 
  1. class MyIntegral(Integral):
  2. def __add__(self, other):
  3. if isinstance(other, MyIntegral):
  4. return do_my_adding_stuff(self, other)
  5. elif isinstance(other, OtherTypeIKnowAbout):
  6. return do_my_other_adding_stuff(self, other)
  7. else:
  8. return NotImplemented
  9. def __radd__(self, other):
  10. if isinstance(other, MyIntegral):
  11. return do_my_adding_stuff(other, self)
  12. elif isinstance(other, OtherTypeIKnowAbout):
  13. return do_my_other_adding_stuff(other, self)
  14. elif isinstance(other, Integral):
  15. return int(other) + int(self)
  16. elif isinstance(other, Real):
  17. return float(other) + float(self)
  18. elif isinstance(other, Complex):
  19. return complex(other) + complex(self)
  20. else:
  21. return NotImplemented

Complex 有 5 種不同的混合類型的操作。 我將上面提到的所有代碼作為“模板”稱作 MyIntegralOtherTypeIKnowAbout。 a 是 Complex 的子類型 A 的實例 (a : A <: Complex),同時 b : B <: Complex。 我將要計算 a + b:

  1. 如果 A 被定義成一個承認 b__add__(),一切都沒有問題。

  2. 如果 A 轉回成“模板”失敗,它將返回一個屬于 __add__() 的值,我們需要避免 B 定義了一個更加智能的 __radd__(),因此模板需要返回一個屬于 __add__() 的 NotImplemented 。(或者 A 可能完全不實現 __add__() 。)

  3. 接著看 B__radd__() 。如果它承認 a ,一切都沒有問題。

  4. 如果沒有成功回退到模板,就沒有更多的方法可以去嘗試,因此這里將使用默認的實現。

  5. 如果 B <: A , python 在 A.__add__ 之前嘗試 B.__radd__ 。 這是可行的,是通過對 A 的認識實現的,因此這可以在交給 Complex 處理之前處理這些實例。

如果 A <: ComplexB <: Real 沒有共享任何資源,那么適當的共享操作涉及內置的 complex ,并且分別獲得 __radd__() ,因此 a+b == b+a。

由于對任何一直類型的大部分操作是十分相似的,可以定義一個幫助函數,即一個生成后續(xù)或相反的實例的生成器。例如,使用 fractions.Fraction 如下:

 
 
 
 
  1. def _operator_fallbacks(monomorphic_operator, fallback_operator):
  2. def forward(a, b):
  3. if isinstance(b, (int, Fraction)):
  4. return monomorphic_operator(a, b)
  5. elif isinstance(b, float):
  6. return fallback_operator(float(a), b)
  7. elif isinstance(b, complex):
  8. return fallback_operator(complex(a), b)
  9. else:
  10. return NotImplemented
  11. forward.__name__ = '__' + fallback_operator.__name__ + '__'
  12. forward.__doc__ = monomorphic_operator.__doc__
  13. def reverse(b, a):
  14. if isinstance(a, Rational):
  15. # Includes ints.
  16. return monomorphic_operator(a, b)
  17. elif isinstance(a, Real):
  18. return fallback_operator(float(a), float(b))
  19. elif isinstance(a, Complex):
  20. return fallback_operator(complex(a), complex(b))
  21. else:
  22. return NotImplemented
  23. reverse.__name__ = '__r' + fallback_operator.__name__ + '__'
  24. reverse.__doc__ = monomorphic_operator.__doc__
  25. return forward, reverse
  26. def _add(a, b):
  27. """a + b"""
  28. return Fraction(a.numerator * b.denominator +
  29. b.numerator * a.denominator,
  30. a.denominator * b.denominator)
  31. __add__, __radd__ = _operator_fallbacks(_add, operator.add)
  32. # ...

當前名稱:創(chuàng)新互聯Python教程:numbers —- 數字的抽象基類
轉載來源:http://www.5511xx.com/article/djsdpdj.html