Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. class MyCoolClass(object):
  2.  
  3. def __init__(self, name):
  4. self.name = name
  5.  
  6.  
  7. foo = MyCoolClass("jordan")
  8.  
  9. print(foo.name)
  10.  
  11. try:
  12. foo.nothere
  13. except AttributeError:
  14. print("oops, that didn't work")
  15.  
  16. ## Output:
  17. ## jordan
  18. ## oops, that didn't work
  19.  
  20. class MyCoolerClass(object):
  21. def __init__(self, name):
  22. self.name = name
  23.  
  24. def __getitem__(self, item):
  25. return "I dunno what %s is" % item
  26.  
  27. def __getattr__(self, attr):
  28. return "you want whaat? %s??" % attr
  29.  
  30. foo = MyCoolerClass("vishal")
  31.  
  32. print(foo.name)
  33. print(foo.nothere)
  34. print(foo[1])
  35.  
  36. ## Output:
  37. ## vishal
  38. ## you want whaat? nothere??
  39. ## I dunno what 1 is
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement