Advertisement
Brainsucker

Untitled

Sep 3rd, 2012
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1.  
  2. class Power(float):
  3.     def __init__(self):
  4.         self.phys_unit = 'Watt'
  5.         self.formal_letter = 'P'
  6.         self.abbreviation = 'W'
  7.        
  8.     def __mul__(self, other):
  9.         pass
  10.    
  11.     def __div__(self, other):
  12.         if isinstance(other, Voltage):
  13.             return Current(self / other)
  14.            
  15.         elif isinstance(other, Current):
  16.             return Voltage(self / other)
  17.  
  18.  
  19. class Resistance(float):
  20.     def __init__(self):
  21.         self.phys_unit = 'Ohm'
  22.         self.formal_letter = 'R'
  23.         self.abbreviation = 'Ω'
  24.        
  25.     def __mul__(self, other):
  26.         if isinstance(other, Current):
  27.             return Voltage(self * other)
  28.        
  29.     def __div__(self, other):
  30.         pass
  31.  
  32.  
  33. class Current(float):
  34.     def __init__(self):
  35.         self.phys_unit = 'Ampere'
  36.         self.formal_letter = 'I'
  37.         self.abbreviation = 'A'
  38.      
  39.     def __mul__(self, other):
  40.         if isinstance(other, Voltage):
  41.             return Power(self * other)
  42.    
  43.     def __div__(self, other):
  44.         pass
  45.  
  46. class Voltage(float):
  47.     def __init__(self):
  48.         self.phys_unit = 'Volt'
  49.         self.formal_letter = 'U'
  50.         self.abbreviation = 'V'
  51.  
  52.     def __mul__(self, other):
  53.         if isinstance(other, Current):
  54.             return Power(self * other)
  55.            
  56.            
  57.     def __div__(self, other):
  58.         if isinstance(other, Current):
  59.             return Resistance(self / other)
  60.            
  61.         elif isinstance(other, Resistance):
  62.             return Current(self / other)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement