![]() |
Native support for array-slicing syntax? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Native support for array-slicing syntax? (/thread-17731.html) |
Native support for array-slicing syntax? - nxs - Apr-22-2019 Hello, A newbie question on the 'how' of the array slicing syntax. Given that,
For example, Quote:>>> from numpy import * In other words, if I were to create a NumPy-like package from scratch, what feature of Python would I use to support the colon-based syntax for slicing? Many thanks. Regards, /nxs RE: Native support for array-slicing syntax? - ichabod801 - Apr-22-2019 I don't know exactly how Numpy does it. But you could do it for your own classes by overriding __getitem__(key) and __setitem__(key, value). Whatever is in the brackets gets passed to those methods as the key parameter. In the example you give, it's a tuple of two slices. But as far as I know, it could be anything. And you could do anything with it. You could have it be the string 'one to five' and write code that would interpret that as 1:5 and return the appropriate slice of whatever. |