Advertisement
treyhunner

list iadd and tuple setitem

Feb 16th, 2016
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. # This:
  2. # >>> a[0] += ['hi']
  3. #
  4. # Is pretty much the same as this:
  5. # >>> x = a[0]
  6. # >>> x += ['hi']
  7. # >>> a[0] = x
  8.  
  9.  
  10. class Tuple(tuple):
  11.     def __setitem__(self, *args, **kwargs):
  12.         print("TUPLE __setitem__")
  13.         return super().__setitem__(*args, **kwargs)
  14.  
  15.  
  16. class List(list):
  17.     def __iadd__(self, *args, **kwargs):
  18.         print("LIST __iadd__")
  19.         return super().__iadd__(*args, **kwargs)
  20.  
  21. # >>> x = List()
  22. # >>> y = Tuple((x,))
  23. # >>> y[0] += List([1])
  24. # LIST __iadd__
  25. # TUPLE __setitem__
  26. # Traceback (most recent call last):
  27. #   File "<stdin>", line 1, in <module>
  28. #   File "<stdin>", line 4, in __setitem__
  29. # AttributeError: 'super' object has no attribute '__setitem__'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement