To determine when __get__
and __set__
is called, try this class:
from collections.abc import MutableSequence, Iterable
class PrintingMutable(MutableSequence):
def __init__(self, source: (MutableSequence, Iterable) = None):
try:
self.arr = list(source)
except TypeError:
self.arr = []
def __repr__(self):
return repr(self.arr)
def insert(self, index: int, o) -> None:
self.arr.insert(index, o) # fail-fast
self.on_insert(index, o)
def __getitem__(self, i: (int, slice)):
val = self.arr[i] # creating unnecessary name to fail-fast.
if isinstance(i, slice):
for idx in range(i.start, i.stop, i.step if i.step else 1):
self.on_get(idx)
else:
self.on_get(i)
return val
def __setitem__(self, i: int, o) -> None:
self.arr[i] = o
self.on_set(i)
def __delitem__(self, i: int) -> None: # I don't think I'm gonna use it.
del self.arr[i]
self.on_del(i)
def __len__(self) -> int:
return len(self.arr)
def on_insert(self, idx, item):
print(f"insert item {item} at {idx}")
self.print_statistics()
def on_get(self, idx):
print(f"get item {self.arr[idx]} at {idx}")
self.print_statistics()
def on_set(self, idx):
print(f"set item {self.arr[idx]} at {idx}")
self.print_statistics()
def on_del(self, idx):
print(f"del item {self.arr[idx]} at {idx}")
self.print_statistics()
def print_statistics(self):
print(self)
And play around in Python console - you'll see the sequence python evaluate your question.
Added comments to clarify steps.
a[0], a[1] = a[1], a[0]
get item 0 at 1
[1, 0]
get item 1 at 0
[1, 0]
set item 0 at 0
[0, 0]
set item 1 at 1
[0, 1]
Normally you'd expect __getitem__
and __setitem__
get/assign value in same order. For this case - get 0, get 1, set 0, set 1.
For your code:
>>> from _63866011 import PrintingMutable
>>> a = PrintingMutable([1, 0])
>>> a[0], a[a[0]] = a[a[0]], a[0]
get item 1 at 0 # get a[0] = 1
[1, 0]
get item 0 at 1 # get a[a[0]] = 0
[1, 0]
get item 1 at 0
[1, 0]
set item 0 at 0
[0, 0]
get item 0 at 0
[0, 0]
set item 1 at 0
[1, 0]
get 1, get 2, set 1, set 2, at same order just like above.
But use of list item to access list is messing up with indices.
Keep playing with this until you've got idea how assignment works in MutableSequence
.