最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python中的數(shù)學運算操作符使用進階

 更新時間:2016年06月20日 18:19:18   作者:catHeart  
這篇文章主要介紹了Python中的數(shù)學運算操作符使用進階,也包括運算賦值操作符等基本知識的小結(jié),需要的朋友可以參考下

Python中對象的行為是由它的類型 (Type) 決定的。所謂類型就是支持某些特定的操作。數(shù)字對象在任何編程語言中都是基礎(chǔ)元素,支持加、減、乘、除等數(shù)學操作。
Python的數(shù)字對象有整數(shù)和浮點數(shù),支持各種數(shù)學操作,比如+, -,*, /等。 沒有這些操作符,程序中只能使用函數(shù)調(diào)用的方式進行數(shù)學運算,比如add(2, 3), sub(5, 2)。
程序中操作符的作用與普通數(shù)學操作的用法是一致的,使用中更加簡便直觀。Python中,這些操作符實現(xiàn)是通過定義一些object的特殊方法實現(xiàn)的,比如object.__add__()和object.__sub__()。如果用戶在自己定義類時實現(xiàn)上述特殊方法,可以使自定義類的對象支持相應的數(shù)學操作,從而模擬數(shù)字對象的行為。這其實是達到了操作符重載的效果。

這里通過實現(xiàn)一個具有支持加法運算的中文數(shù)字類說明如何在Python中實現(xiàn)一個支持常見的數(shù)學操作。ChineseNumber類的基本定義如下。

class ChineseNumber:
  def __init__(self, n):
    self.num = n
    self.alphabet = [u'零', u'一', u'二', u'三', u'四', 
      u'五', u'六', u'七', u'八', u'九', u'十']

  def __str__(self):
    sign = '負' if self.num < 0 else ''
    return sign + ''.join([self.alphabet[int(s)] for s in str(abs(self.num))])

  def __repr__(self):
    return self.__str__()

目前,實現(xiàn)的效果是這樣的:

>>> a = ChineseNumber(2)
>>> a  #調(diào)用a.__repr__()
二
>>> print(a)  #調(diào)用a.__str__()

二

一般數(shù)學操作符
定義類時,實現(xiàn)__add__()方法,可以給這個類增加+操作符。給ChineseNumber增加如下方法:

  def __add__(self, other):
    if type(other) is ChineseNumber:
      return ChineseNumber(self.num + other.num)
    elif type(other) is int:
      return ChineseNumber(self.num + other)
    else:
      return NotImplemented

這時ChineseNumber的對象就可以使用+了。

>>> a, b = ChineseNumber(2), ChineseNumber(10)
>>> a + b
十二
>>> a + 5
七
>>> a + 3.7
TypeError: unsupported operand type(s) for +: 'ChineseNumber' and 'float'

對于+,a + b相當于調(diào)用a.__add__(b). 類似地,可以定義其他數(shù)學操作符,見下表。

object.__add__(self, other): +
object.__sub__(self, other): -
object.__mul__(self, other): *
object.__matmul__(self, other): @
object.__truediv__(self, other): /
object.__floordiv__(self, other): //
object.__mod__(self, other): %
object.__divmod__(self, other): divmod, divmod(a, b) = (a/b, a%b)
object.__pow__(self, other[,modulo]): **, pow()
object.__lshift__(self, other): <<
object.__rshift__(self, other): >>
object.__and__(self, other): &
object.__xor__(self, other): ^
object.__or__(self, other): |

操作數(shù)反轉(zhuǎn)的數(shù)學操作符 (Reflected/Swapped Operand)

>>> 2 + a
TypeError: unsupported operand type(s) for +: 'int' and 'ChineseNumber'

2是整數(shù)類型,它的__add__()方法不支持ChineseNumber類的對象,所以出現(xiàn)了上述錯誤。定義操作數(shù)反轉(zhuǎn)的數(shù)學操作符可以解決這個問題。給ChineseNumber類添加__radd__()方法,實現(xiàn)操作數(shù)反轉(zhuǎn)的+運算。

  def __radd__(self, other):
    return self.__add__(other)

對于a + b,如果a沒有定義__add__()方法,Python嘗試調(diào)用b的__radd__()方法。此時,a + b相當于調(diào)用b.__radd__(a)。

>>> a = ChineseNumber(2)
>>> 2 + a
四

類似地,可以定義其他操作數(shù)反轉(zhuǎn)的數(shù)學操作符,見下表。

object.__radd__(self, other): +
object.__rsub__(self, other): -
object.__rmul__(self, other): *
object.__rmatmul__(self, other): @
object.__rtruediv__(self, other): /
object.__rfloordiv__(self, other): //
object.__rmod__(self, other): %
object.__rdivmod__(self, other): divmod, divmod(a, b) = (b/a, b%a)
object.__rpow__(self, other[,modulo]): **, pow()
object.__rlshift__(self, other): <<
object.__rrshift__(self, other): >>
object.__rand__(self, other): &
object.__rxor__(self, other): ^
object.__ror__(self, other): |

運算賦值操作符
運算賦值操作符使用單個操作符完成運算和賦值操作,比如a += b相當于調(diào)用a = a + b。為ChineseNumber增加__iadd__()方法,可以實現(xiàn)+=操作符。

  def __iadd__(self, other):
    if type(other) is ChineseNumber:
      self.num += other.num
      return self
    elif type(other) is int:
      self.num += other
      return self
    else:
      return NotImplemented

此時,

>>> a, b = ChineseNumber(2), ChineseNumber(10)
>>> a += b
>>> a
十二
>>> a + 7
>>> a
十九

類似地,可以定義其他運算賦值操作符,見下表。

object.__iadd__(self, other): +=
object.__isub__(self, other): -=
object.__imul__(self, other): *=
object.__imatmul__(self, other): @=
object.__itruediv__(self, other): /=
object.__ifloordiv__(self, other): //=
object.__imod__(self, other): %=
object.__ipow__(self, other[,modulo]): **=
object.__ilshift__(self, other): <<=
object.__irshift__(self, other): >>=
object.__iand__(self, other): &=
object.__ixor__(self, other): ^=
object.__ior__(self, other): |=

一元數(shù)學操作符
一元數(shù)學操作符是只有一個操作數(shù)的運算,比如取負數(shù)的操作符-。-對應的特殊函數(shù)是__neg__()。為ChineseNumber添加__neg__()方法,

  def __neg__(self):
    return ChineseNumber(-self.num)

此時,ChineseNumber對象就支持-操作了。

>>> a = ChineseNumber(5)
>>> -a
負五

其他一元運算符見下表。

object.__neg__(self): -
object.__pos__(self): +
object.__abs__(self): abs()
object.__invert__(self): ~
object.__complex__(self): complex()
object.__int__(self): int()
object.__float__(self): float()
object.__round__(self): round()
object.__index__(self): operator.index() 

相關(guān)文章

  • Python爬蟲獲取基金凈值信息詳情

    Python爬蟲獲取基金凈值信息詳情

    這篇文章主要介紹了Python爬蟲獲取基金凈值信息詳情,文章基于錢兩篇文章的內(nèi)容圍繞python的相關(guān)資料展開詳細介紹,需要的小伙伴可以參考一下
    2022-05-05
  • kali最新國內(nèi)更新源sources

    kali最新國內(nèi)更新源sources

    這篇文章主要介紹了kali最新國內(nèi)更新源sources的相關(guān)資料,需要的朋友可以參考下
    2023-03-03
  • 如何用python處理excel表格

    如何用python處理excel表格

    在本篇文章里小編給大家整理了關(guān)于python處理excel表格的詳細步驟內(nèi)容,需要的朋友們可以參考下。
    2020-06-06
  • 機器學習、深度學習和神經(jīng)網(wǎng)絡之間的區(qū)別和聯(lián)系

    機器學習、深度學習和神經(jīng)網(wǎng)絡之間的區(qū)別和聯(lián)系

    機器學習>神經(jīng)網(wǎng)絡>深度學習≈深度神經(jīng)網(wǎng)絡,機器學習包括了神經(jīng)網(wǎng)絡在內(nèi)的許多算法,而神經(jīng)網(wǎng)絡又可以分為淺度神經(jīng)網(wǎng)絡和深度神經(jīng)網(wǎng)絡,深度學習是使用了深度神經(jīng)網(wǎng)絡的技術(shù),雖然機器學習、深度學習和神經(jīng)網(wǎng)絡是不同的,但在構(gòu)建復雜系統(tǒng)時,許多相關(guān)概念是混合在一起的
    2024-02-02
  • Python包的版本切換和更新方式

    Python包的版本切換和更新方式

    這篇文章主要介紹了Python包的版本切換和更新方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 詳解Python中*args和**kwargs的使用

    詳解Python中*args和**kwargs的使用

    本文我們將通過示例了解Python中*args和?**kwargs的使用方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Python Tkinter之事件處理詳解

    Python Tkinter之事件處理詳解

    事件處理,是 GUI 程序中不可或缺的重要組成部分,相比來說,控件只是組成一臺機器的零部件。本文我們將對 Tkinter 中的事件處理機制做詳細的介紹,需要的可以參考一下
    2022-01-01
  • 深入了解python中的常見錯誤類型與解決

    深入了解python中的常見錯誤類型與解決

    在Python編程過程中,經(jīng)常會遇到各種錯誤,了解這些錯誤的類型以及如何處理它們是成為一位優(yōu)秀的Python開發(fā)者所必備的技能之一,下面就跟隨小編一起學習一下python中的常見錯誤類型吧
    2023-11-11
  • Pytorch的mean和std調(diào)查實例

    Pytorch的mean和std調(diào)查實例

    今天小編就為大家分享一篇Pytorch的mean和std調(diào)查實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • python BytesIO 中 read 用法示例詳解

    python BytesIO 中 read 用法示例詳解

    這篇文章主要介紹了python BytesIO 中 read 用法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-06-06

最新評論

措勤县| 盱眙县| 咸阳市| 酒泉市| 曲沃县| 平阴县| 云龙县| 阜平县| 辽中县| 潜山县| 嘉黎县| 荔浦县| 磐石市| 铅山县| 泰顺县| 耿马| 永修县| 黄大仙区| 江川县| 龙江县| 常德市| 和静县| 三门峡市| 东明县| 宁德市| 五指山市| 厦门市| 云阳县| 阿坝县| 潞西市| 儋州市| 稷山县| 扶风县| 毕节市| 柳河县| 阿坝县| 夏津县| 英山县| 双峰县| 马山县| 湟源县|