In python, how do I create two index slicing for my own matrix class?

廉价感情. 提交于 2019-12-04 17:26:45

Note that __getslice__ is deprecated since version 2.0.

So consider the following minimal example:

class Foo:
    def __getitem__(self, *args):
        print args
f = Foo()
f[1:2,2:4]

This will print:

((slice(1, 2, None), slice(2, 4, None)),)

If you have a look at the data model docs, you'll see that slice objects are:

...used to represent slices when extended slice syntax is used. Special read-only attributes: start is the lower bound; stop is the upper bound; step is the step value; each is None if omitted. These attributes can have any type.

From here it should be pretty clear how to implement your 2 index slice handling.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!