Problem with slices.

Antoon Pardon apardon at forel.vub.ac.be
Mon Oct 3 07:05:18 EDT 2005


I'm for the moment writing two classes.

A table, which is like a list, but can start at any integer.

A tree which is like a dictionary, but will iterate over the
keys in sorted order.

The problem is that I would like to implemet slices but, that
seems to be impossible with how slices are implemented now.

I wrote the following class to test things out.

class Tst:
    def __getitem__(self, key):
        print key

then I called the interpreter and got this:

>>> from tst import Tst
>>> t=Tst()
>>> t[:]
slice(0, 2147483647, None)
>>> t[:9]
slice(0, 9, None)
>>> t[:'ok']
slice(None, 'ok', None)
>>> t['ok':]
slice('ok', None, None)
>>> t[6:]
slice(6, 2147483647, None)
>>> t[1,2]
(1, 2)
>>> t[1,2:]
(1, slice(2, None, None))
>>> t[(1,2):]
slice((1, 2), None, None)


Now suppose tab is a table with indexes from -5 to 12. 

tab[:4]  would have to make a table ranging from -5 to 4
tab[0:4] would have to make a table ranging from  0 to 4.

But each time I would be given the same argument, being
slice(0, 4, None). So I would be unable to distinghuish
between the two.

I don't think it very likely but I could have a table
with indexes from 2147483647 to 2147483700, so having
2147483647 as value that indicated till the end of
the sequence is a bit awkward.

The same problems occur when I have a tree with integer
key values. But even if I don't use integers as keys
I have a problem with what is returned since None is
a valid key and thus it shouldn't be used this way.

-- 
Antoon Pardon



More information about the Python-list mailing list