Advertisement
Guest User

Chris

a guest
Sep 22nd, 2009
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.48 KB | None | 0 0
  1. Python 3.1.1 (r311:74480, Sep 11 2009, 19:55:23)                                    
  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 0x7fb150ada910> <__main__.A object at 0x7fb150ada990>
  54. right eq <__main__.A object at 0x7fb150ada990> <__main__.A object at 0x7fb150ada910>
  55. right eq <__main__.A object at 0x7fb150ada990> <__main__.A object at 0x7fb150ada910>
  56. left eq <__main__.A object at 0x7fb150ada910> <__main__.A object at 0x7fb150ada990>
  57. False
  58. >>> A('left') == B('right')
  59. left eq <__main__.A object at 0x7fb150ada950> <__main__.B object at 0x7fb150ada910>
  60. right eq <__main__.B object at 0x7fb150ada910> <__main__.A object at 0x7fb150ada950>
  61. right eq <__main__.B object at 0x7fb150ada910> <__main__.A object at 0x7fb150ada950>
  62. left eq <__main__.A object at 0x7fb150ada950> <__main__.B object at 0x7fb150ada910>
  63. False
  64. >>> A('left') != A('right')
  65. left ne <__main__.A object at 0x7fb150ada990> <__main__.A object at 0x7fb150ada950>
  66. right ne <__main__.A object at 0x7fb150ada950> <__main__.A object at 0x7fb150ada990>
  67. right ne <__main__.A object at 0x7fb150ada950> <__main__.A object at 0x7fb150ada990>
  68. left ne <__main__.A object at 0x7fb150ada990> <__main__.A object at 0x7fb150ada950>
  69. True
  70. >>> A('left') != B('right')
  71. left ne <__main__.A object at 0x7fb150ada910> <__main__.B object at 0x7fb150ada990>
  72. right ne <__main__.B object at 0x7fb150ada990> <__main__.A object at 0x7fb150ada910>
  73. right ne <__main__.B object at 0x7fb150ada990> <__main__.A object at 0x7fb150ada910>
  74. left ne <__main__.A object at 0x7fb150ada910> <__main__.B object at 0x7fb150ada990>
  75. True
  76. >>>
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement