Re: Negative array indicies and slice()

Discussion in 'Python' started by Robert Kern, Nov 1, 2012.

  1. Robert Kern

    Robert Kern Guest

    On 10/31/12 8:16 PM, Andrew Robinson wrote:
    > On 10/31/2012 02:20 PM, Ian Kelly wrote:
    >> On Wed, Oct 31, 2012 at 7:42 AM, Andrew Robinson wrote:
    >> Then; I'd note: The non-goofy purpose of slice is to hold three data
    >> values; They are either numbers or None. These *normally* encountered
    >> values can't create a memory loop.
    >> So, FOR AS LONG, as the object representing slice does not contain an
    >> explicit GC pair; I move that we mandate (yes, in the current python
    >> implementation, even as a *fix*) that its named members may not be
    >> assigned any objects other than None or numbers....
    >> eg: Lists would be forbidden....
    >> Since functions, and subclasses, can be test evaluated by int(
    >> the_thing_to_try ) and *[] can too,
    >> generality need not be lost for generating nothing or numbers.
    >> PEP 357 requires that anything implementing the __index__ special method be
    >> allowed for slicing sequences (and also that __index__ be used for the
    >> conversion). For the most part, that includes ints and numpy integer types,
    >> but other code could be doing esoteric things with it.

    > I missed something... (but then that's why we're still talking about it...)
    > Reading the PEP, it notes that *only* integers (or longs) are permitted in slice
    > syntax.
    > (Overlooking None, of course... which is strange...)
    > The PEP gives the only exceptions as objects with method "__index__".
    > Automatically, then, an empty list is forbidden (in slice syntax).
    > However, What you did, was circumvent the PEP by passing an empty list directly
    > to slice(), and avoiding running it through slice syntax processing.

    Why do you think it is forbidden by the syntax?

    |1> class A(object):
    ...> def __getitem__(self, key):
    ...> return key

    |2> a = A()

    |3> a[[]:]
    slice([], None, None)

    The PEP is a little unclear and refers to a state of the Python interpreter that
    no longer exists. At the time, I think __getslice__() was still not deprecated,
    and it did require ints (or after the PEP, __index__able objects).
    __getslice__() is now deprecated in favor of __getitem__() where you can
    interpret slice objects with arbitrary objects to your heart's content.
    Arbitrary objects *are* definitely allowed by the slice syntax (how could the
    syntax know what is an int and what is another kind of object?). Most objects
    that interpret slices, especially the builtin sequence types, do require
    __index__able objects (or None).

    > So, what's the psychology behind allowing slice() to hold objects which are not
    > converted to ints/longs in the first place?

    In numpy, we (ab)use this freedom for some convenient notation in special
    objects. We have a couple of grid-making convenience objects:

    |5> numpy.mgrid[1.5:2.5:0.1]
    array([ 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1, 2.2, 2.3, 2.4])

    This syntax uses the start:stop:step notation to make a float range. If we use
    an imaginary integer in the "step" slot, mgrid will interpret it as the number
    of items requested instead of the step.

    |6> numpy.mgrid[1.5:2.5:11j]
    array([ 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1, 2.2, 2.3, 2.4, 2.5])

    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
    Robert Kern, Nov 1, 2012
    1. Advertisements

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. Replies:
    88888 Dihedral
    Nov 2, 2012
  2. Andrew Robinson

    Re: Negative array indicies and slice()

    Andrew Robinson, Oct 29, 2012, in forum: Python
    Andrew Robinson
    Oct 29, 2012
  3. Mark Lawrence

    Re: Negative array indicies and slice()

    Mark Lawrence, Oct 29, 2012, in forum: Python
    Mark Lawrence
    Oct 29, 2012
  4. Ethan Furman

    Re: Negative array indicies and slice()

    Ethan Furman, Oct 30, 2012, in forum: Python
    Ethan Furman
    Oct 30, 2012
  5. Dennis Lee Bieber

    Re: Negative array indicies and slice()

    Dennis Lee Bieber, Oct 31, 2012, in forum: Python
    Dennis Lee Bieber
    Oct 31, 2012

Share This Page