Assuming z is passed as a list (in the corresponding python code). The z += 3
can be translated to del z[:3]
which moves the element 3 to 0.
However in python, you need to copy the array before doing that, because with the del-statement, the array gets modified.
In C you are able to access the elements before the pointed index through negative indices. This can be done with an "invisible" offset nested in a class. This offset is always added onto the index when the list is accessed.
The following class demonstrates the behavior.
class pointer_like:
def __init__(self, lst):
self.lst = lst; self.offset = 0
# self[index]
def __getitem__(self, index):
return self.lst[self.offset + index]
# self += offset
def __iadd__(self, offset):
self.offset += offset
# other member functions...
# as your example above
def somename(z):
z = pointer_like(z)
while (....):
a += z[0]
b += z[1]
c += z[2]
z += 3
>>> # other example
>>> z = pointer_like([0, 1, 2, 3, 4, 5])
>>> z[0]
0
>>> z += 3
>>> z[0]
3
>>>
>>> # with normal python lists, this would mean third last element
>>> z[-3]
0
>>> z += -5
>>> z[2]
0
>>>
>>> # this is special, because z.offset is negative (-2),
>>> # when a list item is accessed through a negative index,
>>> # it is counted from the end of the array in python.
>>> # In this case, it is -2, so the second last is accessed
>>> # In C this would cause undefined behavor, on most
>>> # platforms this causes an access violation
>>> z[0]
4
Note that pyhon also has a +=
operator for lists, but this allows to append another list at the end.