Problem with slices.
__peter__ at web.de
Mon Oct 3 13:25:03 CEST 2005
Antoon Pardon wrote:
> 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
> slice(0, 2147483647, None)
> slice(0, 9, None)
> slice(None, 'ok', None)
> slice('ok', None, None)
> slice(6, 2147483647, None)
> (1, 2)
> (1, slice(2, None, None))
> 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.
Consider new-style classes:
>>> class T(object):
... def __getitem__(self, key):
... return key
>>> t = T()
slice(None, 4, None)
slice(0, 4, None)
slice(0, None, None)
More information about the Python-list