Guest User

Chris

a guest
Sep 22nd, 2009
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.97 KB | None | 0 0
  1. Python 2.6.2 (r262:71600, Sep 11 2009, 20:40:48)                                    
  2. [GCC 4.3.2] on linux2                                                              
  3. Type "help", "copyright", "credits" or "license" for more information.              
  4. >>> from __future__ import print_function                                          
  5. >>> class A(object):                                                                
  6. ...     def __init__(self, name):                                                  
  7. ...             self.name = name                                                    
  8. ...     def __eq__(self, other):                                                    
  9. ...             print(self.name, "eq", self, other)                                
  10. ...             return NotImplemented                                              
  11. ...     def __ne__(self, other):                                                    
  12. ...             print(self.name, "ne", self, other)                                
  13. ...             return NotImplemented                                              
  14. ...     def __add__(self, other):                                                  
  15. ...             print(self.name, "add", self, other)                                
  16. ...             return NotImplemented                                              
  17. ...     def __radd__(self, other):                                                  
  18. ...             print(self.name, "radd", self, other)                              
  19. ...             return NotImplemented                                              
  20. ...                                                                                
  21. ...     def __lt__(self, other):                                                    
  22. ...             print(self.name, "lt", self, other)                                
  23. ...             return NotImplemented                                              
  24. ...     def __gt__(self, other):                                                    
  25. ...             print(self.name, "gt", self, other)                                
  26. ...             return NotImplemented                                              
  27. ...                                                                                
  28. ...                                                                                
  29. >>> class B(object):                                                                
  30. ...     def __init__(self, name):                                                  
  31. ...             self.name = name                                                    
  32. ...     def __eq__(self, other):                                                    
  33. ...             print(self.name, "eq", self, other)                                
  34. ...             return NotImplemented                                              
  35. ...     def __ne__(self, other):                                                    
  36. ...             print(self.name, "ne", self, other)                                
  37. ...             return NotImplemented                                              
  38. ...     def __add__(self, other):                                                  
  39. ...             print(self.name, "add", self, other)                                
  40. ...             return NotImplemented                                              
  41. ...     def __radd__(self, other):                                                  
  42. ...             print(self.name, "radd", self, other)                              
  43. ...             return NotImplemented                                              
  44. ...                                                                                
  45. ...     def __lt__(self, other):                                                    
  46. ...             print(self.name, "lt", self, other)                                
  47. ...             return NotImplemented                                              
  48. ...     def __gt__(self, other):                                                    
  49. ...             print(self.name, "gt", self, other)                                
  50. ...             return NotImplemented
  51. ...
  52. >>> A('left') == A('right')
  53. left eq <__main__.A object at 0x7f7df01663d0> <__main__.A object at 0x7f7df0166410>
  54. right eq <__main__.A object at 0x7f7df0166410> <__main__.A object at 0x7f7df01663d0>
  55. left eq <__main__.A object at 0x7f7df01663d0> <__main__.A object at 0x7f7df0166410>
  56. right eq <__main__.A object at 0x7f7df0166410> <__main__.A object at 0x7f7df01663d0>
  57. right eq <__main__.A object at 0x7f7df0166410> <__main__.A object at 0x7f7df01663d0>
  58. left eq <__main__.A object at 0x7f7df01663d0> <__main__.A object at 0x7f7df0166410>
  59. False
  60. >>> A('left') == B('right')
  61. left eq <__main__.A object at 0x7f7df0166410> <__main__.B object at 0x7f7df01663d0>
  62. right eq <__main__.B object at 0x7f7df01663d0> <__main__.A object at 0x7f7df0166410>
  63. right eq <__main__.B object at 0x7f7df01663d0> <__main__.A object at 0x7f7df0166410>
  64. left eq <__main__.A object at 0x7f7df0166410> <__main__.B object at 0x7f7df01663d0>
  65. False
  66. >>> A('left') != A('right')
  67. left ne <__main__.A object at 0x7f7df01663d0> <__main__.A object at 0x7f7df0166410>
  68. right ne <__main__.A object at 0x7f7df0166410> <__main__.A object at 0x7f7df01663d0>
  69. left ne <__main__.A object at 0x7f7df01663d0> <__main__.A object at 0x7f7df0166410>
  70. right ne <__main__.A object at 0x7f7df0166410> <__main__.A object at 0x7f7df01663d0>
  71. right ne <__main__.A object at 0x7f7df0166410> <__main__.A object at 0x7f7df01663d0>
  72. left ne <__main__.A object at 0x7f7df01663d0> <__main__.A object at 0x7f7df0166410>
  73. True
  74. >>> A('left') != B('right')
  75. left ne <__main__.A object at 0x7f7df0166410> <__main__.B object at 0x7f7df01663d0>
  76. right ne <__main__.B object at 0x7f7df01663d0> <__main__.A object at 0x7f7df0166410>
  77. right ne <__main__.B object at 0x7f7df01663d0> <__main__.A object at 0x7f7df0166410>
  78. left ne <__main__.A object at 0x7f7df0166410> <__main__.B object at 0x7f7df01663d0>
  79. True
  80. >>>
Advertisement
Add Comment
Please, Sign In to add comment