python魔法方法-單目運算及通常算數運算

  在比較的魔法方法中,咱們討論了魔法方法其實就是重載了操做符,例如>、<、==等。而這裏,咱們繼續討論有關於數值的魔法方法。python

1.單目運算符或單目運算函數  

  • __pos__(self)函數

  • 實現一個取正數的操做(好比 +some_object ,python調用__pos__函數)spa

  • __neg__(self).net

  • 實現一個取負數的操做(好比 -some_object )3d

  • __abs__(self)code

  • 實現一個內建的abs()函數的行爲blog

  • __invert__(self)get

  • 實現一個取反操做符(~操做符)的行爲。it

  • __round__(self, n)io

  • 實現一個內建的round()函數的行爲。 n 是待取整的十進制數.(貌似在2.7或其餘新版本中廢棄)

  • __floor__(self)

  • 實現math.floor()的函數行爲,好比, 把數字下取整到最近的整數.(貌似在2.7或其餘新版本中廢棄)

  • __ceil__(self)

  • 實現math.ceil()的函數行爲,好比, 把數字上取整到最近的整數.(貌似在2.7或其餘新版本中廢棄)

  • __trunc__(self)

  • 實現math.trunc()的函數行爲,好比, 把數字截斷而獲得整數.

class Foo(str):
    def __new__(cls, x, *args, **kwargs):
        return super(Foo, cls).__new__(cls, x)

    def __init__(self, x):
        self.x = x

    def __pos__(self):
        return '+' + self.x

    def __neg__(self):
        return '-' + self.x

    def __abs__(self):
        return 'abs:' + self.x

    def __invert__(self):
        return 'invert:' + self.x

a = Foo('scolia')
print +a
print -a
print ~a

 

2.通常算數運算

  • __add__(self, other)

  • 實現一個加法.

  • __sub__(self, other)

  • 實現一個減法.

  • __mul__(self, other)

  • 實現一個乘法.

  • __floordiv__(self, other)

  • 實現一個「//」操做符產生的整除操做

  • __div__(self, other)

  • 實現一個「/」操做符表明的除法操做.(由於Python 3裏面的division默認變成了true division,__div__在Python3中不存在了)

  • __truediv__(self, other)

  • 實現真實除法,注意,只有當你from __future__ import division時纔會有效。

  • __mod__(self, other) 

    實現一個「%」操做符表明的取模操做.

  • __divmod__(self, other)

  • 實現一個內建函數divmod()

  • __pow__

  • 實現一個指數操做(「**」操做符)的行爲

  • __lshift__(self, other)

  • 實現一個位左移操做(<<)的功能

  • __rshift__(self, other)

  • 實現一個位右移操做(>>)的功能.

  • __and__(self, other)

  • 實現一個按位進行與操做(&)的行爲.

  • __or__(self, other) 

    實現一個按位進行或操做(|)的行爲.

  • __xor__(self, other)

  • 實現一個異或操做(^)的行爲

 

class Foo(str):
    def __new__(cls, x, *args, **kwargs):
        return super(Foo, cls).__new__(cls, x)

    def __init__(self, x):
        self.x = x

    def __add__(self, other):
        return self.x + '+' + other.x

    def __sub__(self, other):
        return self.x + '-' + other.x

    def __mul__(self, other):
        return self.x + '*' + other.x

    def __floordiv__(self, other):
        return self.x  + '//' + other.x

    def __div__(self, other):
        return self.x + '/' + other.x

    def __truediv__(self, other):
        return self.x + 't/' + other.x

    def __mod__(self, other):
        return self.x + '%' + other.x

    def __divmod__(self, other):
        return self.x + 'divmod' + other.x

    def __pow__(self, power, modulo=None):
        return self.x + '**' + str(power)

    def __lshift__(self, other):
        return self.x + '<<' + other.x

    def __rshift__(self, other):
        return self.x + '>>' + other.x

    def __and__(self, other):
        return self.x + '&' + other.x

    def __or__(self, other):
        return self.x + '|' + other.x

    def __xor__(self, other):
        return self.x + '^' + other.x

a = Foo('scolia')
b = Foo('good')
print a + b
print a - b
print a * b
print a // b
print a / b
print a % b
print divmod(a, b)
print a ** b
print a << b
print a >> b
print a & b
print a | b
print a ^ b

from __future__ import division
.......

print a / b

 


 

  歡迎你們交流

  參考資料:戳這裏

相關文章
相關標籤/搜索