zoukankan      html  css  js  c++  java
  • python魔法方法-单目运算及一般算数运算

      在比较的魔法方法中,我们讨论了魔法方法其实就是重载了操作符,例如>、<、==等。而这里,我们继续讨论有关于数值的魔法方法。

    1.单目运算符或单目运算函数  

    • __pos__(self)

    • 实现一个取正数的操作(比如 +some_object ,python调用__pos__函数)

    • __neg__(self)

    • 实现一个取负数的操作(比如 -some_object )

    • __abs__(self)

    • 实现一个内建的abs()函数的行为

    • __invert__(self)

    • 实现一个取反操作符(~操作符)的行为。

    • __round__(self, n)

    • 实现一个内建的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


      欢迎大家交流

      参考资料:戳这里

  • 相关阅读:
    ES6笔记
    JavaScriptOOP (三)
    JavaScriptOOP(二)
    JavaScriptOOP(一)
    基于vue-cli 将webpack3 升级到 webpack4 配置
    JavaScript 中 call,apply 和 bind
    vue 2.6 更新变动
    JavaScript严格模式
    vue 修饰符 整理
    webpack打包将配置文件单独抽离不压缩打包
  • 原文地址:https://www.cnblogs.com/scolia/p/5682013.html
Copyright © 2011-2022 走看看