美文网首页
Python操作符一览

Python操作符一览

作者: slords | 来源:发表于2017-08-02 11:51 被阅读0次
    操作 语法 函数
    加法 a + b __add__(self, value)
    级联 seq1 + seq2 __concat__(self, value)
    包含检测 obj in seq __contains__(self, value)
    除法 a / b __truediv__(self, value)
    整数除 a // b __floordiv__(self, value)
    按位取和 a & b __and__(self, value)
    按位取异或 a ^ b __xor__(self, value)
    按位取非 ~ a __invert__(self)
    按位取或 a | b __or__(self, value)
    幂级数 a ** b __pow__(self, value)
    校验 a is b __is__(self, value)
    校验(非) a is not b __is_not__(self, value)
    按目录设置 obj[k] = v __setitem__(self, k, v)
    按目录删除 del obj[k] __delitem__(obj, k)
    按目录获取 obj[k] __getitem__(obj, k)
    左移位 a << b __lshift__(self, value)
    取余 a % b __mod__(self, value)
    乘法 a * b __mul__(self, value)
    矩阵乘法 a @ b __matmul__(self, value)
    取反(算数) - a __neg__(self)
    取反(逻辑) not a __not__(self)
    取正 + a __pos__(self)
    右移位 a >> b __rshift__(self, value)
    片段设置 seq[i:j] = values __setitem__(self, slice__(i, j), values)
    片段删除 del seq[i:j] __delitem__(self, slice__(i, j))
    片段获取 seq[i:j] __getitem__(self, slice__(i, j))
    取余 s % obj __mod__(self, value)
    减法 a - b __sub__(self, value)
    对应bool类型 obj __truth__(self)
    比较(小于) a < b __lt__(self, value)
    比较(小于等于) a <= b __le__(self, value)
    等于 a == b __eq__(self, value)
    不等于 a != b __ne__(self, value)
    比较(大于) a >= b __ge__(self, value)
    比较大于等于 a > b __gt__(self, value)

    详细参见:10.3. operator — Standard operators as functions — Python 3.6.2 documentation

    注:可使用定义私有形式的对应函数方法的方式,使任意类型的对象进行操作符运算。如:

    class Phone(object):
    
        def __init__(self, name, price):
             self.name = name
             self.price = price
         
        def __lt__(self,phone):
             return self.price < phone.price
         
        def __le__(self,phone):
             return self.price <= phone.price
         
        def __eq__(self,phone):
             return self.price == phone.price
         
        def __ne__(self,phone):
             return self.price != phone.price
         
        def __ge__(self,phone):
             return self.price > phone.price
         
        def __gt__(self,phone):
             return self.price >= phone.price
     
    if __name__ == "__main__":
        mi5 = Phone('xiaomi',2000)
        iphone = Phone('apple',7000)
        print(mi5 < iphone)      #True    
        print(mi5 <= iphone)     #True
        print(mi5 == iphone)     #False
        print(mi5 != iphone)     #True
        print(mi5 > iphone)      #False
        print(mi5 >= iphone)     #False
    

    另外,可以定义右运算符的操作,这种操作符仅在运算符左侧的实例没有对应的运算符操作定义,且右侧实例有右运算符定义的情况下起效。
    右运算符的函数方法有:
    __radd__、__rsub__、__rmul__、__rfloordiv__、__rtruediv__、__rmod__、__rdivmod__、__rpow__、__rlshift__、__rshift__、__rand__、__ror__、__rxor__
    对于自运算同样有额外的定义方式,即a += 1等。可以定义自运算的方法名有:
    __iadd__、__isub__、__imul__、__ifloordiv__、__itruediv__、__imod__、__ipow__、__ilshift__、__irshift__、__iand__、__ior__、__ixor__

    以上实例方法不能通过__setattribute__设置。需要直接定义起效。

    相关文章

      网友评论

          本文标题:Python操作符一览

          本文链接:https://www.haomeiwen.com/subject/nayelxtx.html