Indexing and Slices

Although the indexing and slicing in Python and MATLAB allow similar flexibility there is one important difference that makes translation of MATLAB code difficult. Most of modern programming languages base their indexing on 0. Python is one of the languages where by default indexing should be assumed based on 0. However thanks to operator overloading it is possible to define a completelly different behavior.

One approach to this problem is to simply subtract 1 from every number inside of brackets [], but this

for i = 1:10, a(i,i) = 1; end
for i in mslice_(1,10): a[i-1,i-1]

This way we don't introduce any extra steps for the Python interpreter. The syntax a[1:10] is translated into a.__getitem__(slice(1,10)), which we write directly while misusing the __call__ function to act as a __getitem__ replacement. So our translation look like a(mslice_(1,10)).
There is 1 extra reason why not to
Comments