furas

Python - __str__ & __repr__ (FB: learnpython.org)

Oct 19th, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. class Test:
  2.     pass
  3.  
  4. obj = Test()
  5. print(' str:', obj)
  6. print('repr:', repr(obj))
  7.  
  8. #  str: <__main__.Test object at 0x7fbdab39d128>
  9. # repr: <__main__.Test object at 0x7fbdab39d128>
  10.  
  11. # ------------------------------------------------
  12.  
  13. class Test:
  14.     def __str__(self):
  15.         return "Hello"
  16.  
  17. obj = Test()
  18. print(' str:', obj)
  19. print('repr:', repr(obj))
  20.  
  21. #  str: Hello
  22. # repr: <__main__.Test object at 0x7fbdab46c518>
  23.  
  24. # ------------------------------------------------
  25.  
  26. class Test:
  27.     def __str__(self):
  28.         return "Hello"
  29.     def __repr__(self):
  30.         return "Good Bye"
  31.  
  32. obj = Test()
  33. print(' str:', obj)
  34. print('repr:', repr(obj))
  35.  
  36. #  str: Hello
  37. # repr: Good Bye
  38.  
  39. # ------------------------------------------------
  40.  
  41. class Test:
  42.     def __repr__(self):
  43.         return "Good Bye"
  44.  
  45. obj = Test()
  46. print(' str:', obj)
  47. print('repr:', repr(obj))
  48.  
  49. #  str: Good Bye
  50. # repr: Good Bye
Add Comment
Please, Sign In to add comment