Problem with slices.

A

Antoon Pardon

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.
 
P

Peter Otten

Antoon said:
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.


Consider new-style classes:
.... def __getitem__(self, key):
.... return key
....
t = T()
t[:4] slice(None, 4, None)
t[0:4] slice(0, 4, None)
t[0:]
slice(0, None, None)

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top