
281
7.8 特殊メソッド
表
7-5
二項演算子(演算子の右のオブジェクトの場合)
式 メソッド
a + b __radd__(self, other)
a - b __rsub__(self, other)
a * b __rmul__(self, other)
a / b __rtruediv__(self, other)
a // b __rfloordiv__(self, other)
a % b __rmod__(self, other)
divmod(a, b) __rdivmod__(self, other)
pow(a, b, c), a ** b __rpow__(self, other)
a << b __rlshift__(self, other)
a >> b __rrshift__(self, other)
a & b __rand__(self, other)
a ^ b __rxor__(self, other)
a | b __ror__(self, other)
class Spam:
val = ''
def __radd__(self, other):
"""1 + Spam()
などで呼び出される
"""
return self.val + other
7.8.2
単項演算子
表
7-6
単項演算子
式 メソッド
-a __neg__(self)
+a __pos___(self)
~a __invert___(self)
abs(a) __abs___(self)
-obj
や
~obj
などの、単項演算子で呼び出されます。 ...