Created
July 20, 2018 05:49
-
-
Save walkingpendulum/093767a780a969f55f446a584ef24d24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Tuple(tuple): | |
def __setitem__(self, key, value): | |
print('tuple setitem', key, value) | |
super().__setitem__(key, value) | |
def __getitem__(self, item): | |
print('tuple getitem', item) | |
return super().__getitem__(item) | |
class List(list): | |
def __iadd__(self, other): | |
print('list iadd', other) | |
super().__iadd__(other) | |
print('list state: %s' % self) | |
return self | |
x = Tuple([1, 2, List([10])]) | |
print('x = (1, 2, [10])') | |
try: | |
print('x[2] += [20, 30]...') | |
x[2] += List([20, 30]) | |
finally: | |
print('Finally:', x) | |
# $ python tst.py | |
# x = (1, 2, [10]) | |
# x[2] += [20, 30]... | |
# tuple getitem 2 | |
# list iadd [20, 30] | |
# list state: [10, 20, 30] | |
# tuple setitem 2 [10, 20, 30] | |
# Finally: (1, 2, [10, 20, 30]) | |
# Traceback (most recent call last): | |
# File "tst.py", line 24, in <module> | |
# x[2] += List([20, 30]) | |
# File "tst.py", line 4, in __setitem__ | |
# super().__setitem__(key, value) | |
# AttributeError: 'super' object has no attribute '__setitem__' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment