Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Lists (object):
- def __init__(self):
- self.thelist = [0,0,0]
- Ls = Lists()
- # trying this only gives 't' as the second argument. Python error results.
- # Interesting that you can slice a string to in the getattr/setattr functions
- # Here one could access 'thelist' with with [0:7]
- print getattr(Ls, 'thelist'[0])
- # tried these two as well to no avail.
- # No error message ensues but the list isn't altered.
- # Instead a new variable is created Ls.'' - printed them out to show they now exist.
- setattr(Lists, 'thelist[0]', 3)
- setattr(Lists, 'thelist[0]', 3)
- print Ls.thelist
- print getattr(Ls, 'thelist[0]')
- print getattr(Ls, 'thelist[0]')
- getattr(Ls, 'thelist')[0] = 2
- getattr(Ls, 'thelist')[0].append(3)
- print getattr(Ls, 'thelist')[0]
- l = getattr(Ls, 'thelist')
- l[0] = 2 #for example
- setattr(Ls, 'thelist', l)
- class Lists (object):
- def __init__(self):
- self.thelist = [0,0,0]
- def __getitem__(self, index):
- return self.thelist[index]
- def __setitem__(self, index, value):
- self.thelist[index] = value
- def __repr__(self):
- return repr(self.thelist)
- Ls = Lists()
- print Ls
- print Ls[1]
- Ls[2] = 9
- print Ls
- print Ls[2]
Add Comment
Please, Sign In to add comment