Python中的數學運算操作符使用進階,python操作符

來源:互聯網
上載者:User

Python中的數學運算操作符使用進階,python操作符

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

這裡通過實現一個具有支援加法運算的中文數字類說明如何在Python中實現一個支援常見的數學操作。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__()

目前,實現的效果是這樣的:

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

一般數學操作符
定義類時,實現__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.7TypeError: unsupported operand type(s) for +: 'ChineseNumber' and 'float'

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

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): |

運算元反轉的數學操作符 (Reflected/Swapped Operand)

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

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

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

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

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

類似地,可以定義其他動作數反轉的數學操作符,見下表。

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相當於調用a = a + b。為ChineseNumber增加__iadd__()方法,可以實現+=操作符。

  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): |=

一元數學操作符
一元數學操作符是只有一個運算元的運算,比如取負數的操作符-。-對應的特殊函數是__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() 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.