Advertisement
Guest User

Untitled

a guest
Feb 26th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. # works
  2. class TFloatObject(TObject):
  3. ...
  4. def add(self, other):
  5. assert(isinstance(other, TObject))
  6.  
  7. if isinstance(other, TIntObject):
  8. return TFloatObject(self.val + other.val)
  9. elif isinstance(other, TFloatObject):
  10. return TFloatObject(self.val + other.val)
  11. else:
  12. raise Exception("Incompatibe types")
  13.  
  14.  
  15. # fails
  16. class TFloatObject(TObject):
  17. ...
  18. def add(self, other):
  19. assert(isinstance(other, TObject))
  20.  
  21. if isinstance(other, TIntObject) or isinstance(other, TFloatObject):
  22. return TFloatObject(self.val + other.val)
  23. else:
  24. raise Exception("Incompatibe types")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement