Problem with slices.

Discussion in 'Python' started by Antoon Pardon, Oct 3, 2005.

  1. 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
     
    Antoon Pardon, Oct 3, 2005
    #1
    1. Advertising

  2. Antoon Pardon

    Peter Otten Guest

    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
    >>>> 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:

    >>> class T(object):

    .... 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
     
    Peter Otten, Oct 3, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. proudfeet

    array slices

    proudfeet, Oct 1, 2003, in forum: VHDL
    Replies:
    1
    Views:
    623
    Egbert Molenkamp
    Oct 1, 2003
  2. Griff

    obtaining lengths of slices

    Griff, Aug 4, 2004, in forum: Perl
    Replies:
    6
    Views:
    433
    Gunnar Hjalmarsson
    Aug 6, 2004
  3. Apc
    Replies:
    1
    Views:
    569
    Roedy Green
    Jun 2, 2004
  4. Apc
    Replies:
    1
    Views:
    855
  5. Alex Polite

    No Array slices! What is this?

    Alex Polite, Jun 6, 2004, in forum: Java
    Replies:
    3
    Views:
    522
    John C. Bollinger
    Jun 7, 2004
Loading...

Share This Page