python - How to do a math operation in both directions in a class method (i.e.: A * 2 and 2 * A)? -


so trying program can add, subtract , multiply volumes (which consist of magnitude , unit). got add work, have not figured out how make rsub , mult work.

i want able operations like. volume * 2 or 2 * volume .i.e: = volume(10,"ml") then: 2 * a.

any advice on how go appreciated! (the weird indentation because copied code here , formatted in weird way)(i didn't include is_valid, can add though).

def __init__ (self, m = 0, u = "ml"):     self.__magnitude = m     self.__units = u  def __add__ (self, v2): #adds magnitude using int or volume     if volume.is_valid(self):         if isinstance(v2, volume):             if volume.is_valid(v2): #check if v2 valid                 if self.__units == v2.__units:                     return volume(self.__magnitude + v2.__magnitude,                                    self.__units)                 else:                     if v2.__units == 'ml':                         v3 = v2.customary()                         return volume(self.__magnitude + v3.__magnitude,                                        self.__units)                     elif v2.__units == 'oz':                         v3 = v2.__units                         return volume(self.__magnitude + v3.__magnitude,                                        self.__units)          elif isinstance(v2,(int,float)):             #create new volume same units self             v3 = volume(v2, self.__units)             return volume(self.__magnitude + v3.__magnitude, self.__units)  def __radd__ (self, v2): #adds ability i.e. 2 +     return self.__add__(v2)  def __sub__ (self, v2): #same __add__ subtracts     if volume.is_valid(self):         if isinstance(v2, volume):             if volume.is_valid(v2):                 if self.__units == v2.__units:                     return volume(self.__magnitude - v2.__magnitude,                                    self.__units)                 else:                     if v2.__units == 'ml':                         v3 = v2.customary()                         return volume(self.__magnitude - v3.__magnitude,                                        self.__units)                     elif v2.__units == 'oz':                         v3 = v2.__units                         return volume(self.__magnitude - v3.__magnitude,                                        self.__units)         elif isinstance(v2,(int,float)):             v3 = volume(v2, self.__units)             return volume(self.__magnitude - v3.__magnitude, self.__units)  def __rsub__ (self, v2):     return volume(v2.__magnitude - self.__magnitude, self.__units)  def __mult__ (self, v2):#multiplies volume int (not other vol)     if volume.is_valid(self):         if isinstance(v2, volume):             if volume.is_valid(v2):                 if self.__units == v2.__units:                     return volume(self.__magnitude * v2.__magnitude,                                    self.__units)                 else:                     if v2.__units == 'ml':                         v3 = v2.customary()                         return volume(self.__magnitude * v3.__magnitude,                                        self.__units)                     elif v2.__units == 'oz':                         v3 = v2.__units                         return volume(self.__magnitude * v3.__magnitude,                                        self.__units)         elif isinstance(v2,(int,float)):             v3 = volume(v2, self.__units)             return volume(self.__magnitude * v3.__magnitude, self.__units)   def __rmult__ (self, v2):     return self.__mult__(v2) 

use __rmul__ instead of __rmult___ , __mul__ instead of __mult__


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -