[] Object-oriented Python operator overloading

  Object-oriented programming can override operator, so the operator can operate the instance of a class.

The general format overloaded methods as follows:

. 1  DEF  __ operator name __ (Self, OTHER):
 2    Operator Statement

Commonly used operator overloading three types: binary arithmetic operator overload, reverse arithmetic operator overloading, comparison overloaded unary operator overloading

1 two yuan arithmetic operators Overload:
 2  The method of operation and expression were described
 . 3  __add__ (Self, RHS) Self + RHS adder
 . 4  __sub__ (Self, RHS) Self - RHS subtraction
 . 5  __mul__ (Self, RHS) Self * rhs multiplication
 . 6  __truediv__ (Self, rhs) Self / rhs division
 . 7  __floordiv__ (Self, rhs) // Self rhs divisible
 . 8  __mod__ (Self, rhs) Self% rhs modulo
 . 9  __pow__ (Self, rhs) Self rhs ** exponentiation 
* rhs is the meaning of right hand side
A  reverse arithmetic operator overloads: 
  when the left side of the operator for Python built-in data types: time (e.g., int, float, etc.), the right custom object type requires the operator to re-reverse load.
2 The method of operation and expression were described . 3 __radd__ (Self, LHS) LHS + Self adder . 4 __rsub__ (Self, LHS) LHS - Self subtraction . 5 __rmul__ (Self, LHS) LHS * Self multiplication . 6 __rtruediv__ (Self, LHS) LHS / Self division . 7 __rfloordiv__ (Self, LHS) LHS // Self divisible . 8 __rmod__ (Self, LHS) LHS% Self modulo . 9 __rpow__(self, lhs) lhs ** self exponentiation
* lhs Hand Side meaning is left
* a visible symbol name is a reverse common arithmetic operators operator plus r (reverse)
1 Comparative overloaded arithmetic operators:
 two  methods were described operation and expression
 . 3  __lt__ (Self, RHS) Self < RHS less than
 . 4  __le__ (Self, RHS) Self <= RHS less
 . 5  __gt__ (Self, RHS) Self > RHS greater than
 . 6  __ge__ (Self, RHS) Self> = RHS or greater
 . 7  __eq__ (Self, RHS) Self == RHS equal
 . 8  __ne__ (Self, RHS) Self! = not equal RHS
A  unary operator overloads
 two  methods were described operation and expression
 . 3  __neg__ (Self) - Self negative
 . 4  __pos__ (Self) + Self positive sign
 . 5  __invert__ (Self) ~ Self negated
. 1  For example: 
    In Case of scores of operator be overloaded.
2 class Fraction: # Score Class . 3 DEF the __init__ (Self, NUM, Deno): . 4 IF Deno < 0: . 5 Deno = -1 * Deno . 6 NUM = -1 * NUM . 7 H = HCF (NUM, Deno) . 8 Self. int = NUM (NUM / H) . 9 self.deno = int (Deno / H) 10 . 11 DEF __add__ (Self, OTHER): # adder 12 is temp_lcm = lcm(self.deno, other.deno) 13 num = self.num*(temp_lcm/self.deno)+other.num*(temp_lcm/other.deno) 14 deno = temp_lcm 15 return Fraction(num, deno) 16 17 def __sub__(self, other): # 减法 18 temp_lcm = lcm(self.deno, other.deno) 19 num = self.num * (temp_lcm / self.deno) - other.num * (temp_lcm / other.deno) 20 deno = temp_lcm 21 return Fraction(num, deno) 22 23 def __mul__(self, other): # 乘法 24 return Fraction(self.num*other.num, self.deno*other.deno) 25 26 def __truediv__(self, other): # 除法 27 return Fraction(self.num*other.deno, self.deno*other.num) 28 29 def __rtruediv__(self, lhs): # 反向运算符重载 30 return Fraction(lhs*self.deno, self.num) 31 32 def reverse(self): # 取倒数 33 return Fraction(self.deno, self.num) 34 35 DEF prtfloat (Self): # output in floating point form 36 Print (the format (self.num / self.deno, ' .1f ' )) 37 [ 38 is DEF PRT (Self): # output in fractions of 39 Print ( ' {} / {} ' .format (self.num, self.deno))

 

Reference links:

https://blog.csdn.net/zhangshuaijun123/article/details/82149056

Guess you like

Origin www.cnblogs.com/protectione055/p/11816759.html