Guest User

Untitled

a guest
Jul 22nd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. class Lists (object):
  2. def __init__(self):
  3. self.thelist = [0,0,0]
  4.  
  5. Ls = Lists()
  6.  
  7. # trying this only gives 't' as the second argument. Python error results.
  8. # Interesting that you can slice a string to in the getattr/setattr functions
  9. # Here one could access 'thelist' with with [0:7]
  10. print getattr(Ls, 'thelist'[0])
  11.  
  12.  
  13. # tried these two as well to no avail.
  14. # No error message ensues but the list isn't altered.
  15. # Instead a new variable is created Ls.'' - printed them out to show they now exist.
  16. setattr(Lists, 'thelist[0]', 3)
  17. setattr(Lists, 'thelist[0]', 3)
  18. print Ls.thelist
  19. print getattr(Ls, 'thelist[0]')
  20. print getattr(Ls, 'thelist[0]')
  21.  
  22. getattr(Ls, 'thelist')[0] = 2
  23. getattr(Ls, 'thelist')[0].append(3)
  24. print getattr(Ls, 'thelist')[0]
  25.  
  26. l = getattr(Ls, 'thelist')
  27. l[0] = 2 #for example
  28. setattr(Ls, 'thelist', l)
  29.  
  30. class Lists (object):
  31.  
  32. def __init__(self):
  33. self.thelist = [0,0,0]
  34.  
  35. def __getitem__(self, index):
  36. return self.thelist[index]
  37.  
  38. def __setitem__(self, index, value):
  39. self.thelist[index] = value
  40.  
  41. def __repr__(self):
  42. return repr(self.thelist)
  43.  
  44. Ls = Lists()
  45. print Ls
  46. print Ls[1]
  47. Ls[2] = 9
  48. print Ls
  49. print Ls[2]
Add Comment
Please, Sign In to add comment