Wednesday, March 11, 2009

Slice object and Ellipsis


>>> class Array(list):
... def __getitem__(self, key):
... print "key:", key, type(key)
... def __getslice__(self, i, j):
... print "ij", i, j
...
>>> a = Array(range(10))
>>> a[0]
key: 0 <type int="">
>>> a[0:5]
ij 0 5
>>> a[0, 1, 2]
key: (0, 1, 2) <type tuple="">
>>> a[0:6:2]
key: slice(0, 6, 2) <type 'slice'>
>>> a[0:1, 3:4]
key: (slice(0, 1, None), slice(3, 4, None)) <type 'tuple'>
>>> slice(1,3), type(slice(1,3))
(slice(1, 3, None), <type 'slice'>)
>>> a[0, ..., 5]
key: (0, Ellipsis, 5) <type tuple="">
>>> Ellipsis, type(Ellipsis)
(Ellipsis, <type 'ellipsis'>)

hmmm...
(to be updated)

No comments: