Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. class Array(list):
  2. def __getitem__(self, indices):
  3. current = super(self.__class__, self)
  4. for index in indices:
  5. current = current.__getitem__(index)
  6. return current
  7.  
  8. a = Array([[[1,2,3],[4,5,6]],
  9. [[0,1,2],[9,8,7]]])
  10. print a[1,0,2] # 2
  11.  
  12. class Array(list):class Array(list):
  13. def __mapItem(self, index):
  14. # we are creating a function in order to use map()
  15. # which does exactly the same thing as yours
  16. self.current = self.current.__getitem__(index)
  17.  
  18. def __getitem__(self, indices):
  19. self.current = super(self.__class__, self)
  20. # map every index to the target item using the __mapItem function
  21. map(self.__mapItem, indices)
  22. return self.current
  23.  
  24. a = Array([[[1,2,3],[4,5,6]],
  25. [[0,1,2],[9,8,7]]])
  26. print a[1,0,2] # 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement