__setslice__ and classes derived from list

Discussion in 'Python' started by Dave Opstad, Sep 2, 2005.

  1. Dave Opstad

    Dave Opstad Guest

    According to the documentation the __setslice__ method has been
    deprecated since Python 2.0. However, if I'm deriving classes from the
    builtin list class, I've discovered I can't really ignore __setslice__.
    Have a look at this snippet:

    ----------------------------------------------------
    >>> class V(list):

    .... def __setitem__(self, key, value):
    .... if isinstance(key, slice):
    .... print "Slice:", key.start, key.stop, key.step
    .... else:
    .... print "Regular:", key
    .... super(V, self).__setitem__(key, value)
    .... def __setslice__(self, i, j, value):
    .... print "Old method:", i, j
    .... super(V, self).__setslice__(i, j, value)
    ....
    >>> v = V([1,2,4,8])
    >>> v

    [1, 2, 4, 8]
    >>> v[0] = 100

    Regular: 0
    >>> v

    [100, 2, 4, 8]
    >>> v[1:3] = [99, 99]

    Old method: 1 3
    >>> v

    [100, 99, 99, 8]
    >>> v[1:3:1] = [88, 88]

    Slice: 1 3 1
    >>> v

    [100, 88, 88, 8]
    >>> v[-1] = 12

    Regular: -1
    >>> v

    [100, 88, 88, 12]
    >>> v[-3:-1] = [77, 66]

    Old method: 1 3
    >>> v

    [100, 77, 66, 12]
    ----------------------------------------------------

    If I assign to v[1:3] it dispatches via __setslice__, but if I assign to
    v[1:3:1] it dispatches via __setitem__. The documentation states that if
    a __setslice__ method is present it will be used, but if one isn't
    present then a slice will be synthesized and __setitem__ will be used
    exclusively. Since the builtin list class provides a __setslice__
    method, what this means is that any class derived from list still has to
    make provisions for this ostensibly deprecated method.

    There's a workaround for this, namely to include this method:

    def __setslice__(self, i, j, seq):
    self.__setitem__(slice(i, j), seq)

    That way any custom code I need to include in __setitem__ doesn't have
    to be duplicated in __setslice__. But just out of curiosity I thought
    I'd ask the folks here if there's any other way of doing this? Maybe
    something like a "noslicelist" class which doesn't have __setslice__,
    where the standard list class would then be a subclass of noslicelist
    with the __setslice__ method present for compatibility. That way I could
    subclass noslicelist myself, and not have to worry about it.

    Dave
    Dave Opstad, Sep 2, 2005
    #1
    1. Advertising

  2. Dave Opstad wrote:

    > There's a workaround for this, namely to include this method:
    >
    > def __setslice__(self, i, j, seq):
    > self.__setitem__(slice(i, j), seq)
    >
    > That way any custom code I need to include in __setitem__ doesn't have
    > to be duplicated in __setslice__. But just out of curiosity I thought
    > I'd ask the folks here if there's any other way of doing this?


    I don't think so; at least it's what I've always done in the past.
    Perhaps you should submit a feature request? It must be time to get rid
    of __setslice__, if not now, then maybe by Python 3.0.
    --
    Michael Hoffman
    Michael Hoffman, Sep 2, 2005
    #2
    1. Advertising

  3. Dave Opstad

    Dave Opstad Guest

    In article <df9ov7$pdn$>,
    Michael Hoffman <> wrote:

    > Perhaps you should submit a feature request? It must be time to get rid
    > of __setslice__, if not now, then maybe by Python 3.0.


    I'm happy to submit a feature request, once I figure out how to do it!

    Dave
    Dave Opstad, Sep 2, 2005
    #3
  4. Michael Hoffman, Sep 2, 2005
    #4
    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. Colin Goudie
    Replies:
    6
    Views:
    454
    Victor Bazarov
    Jan 26, 2004
  2. Manuel
    Replies:
    8
    Views:
    621
    Manuel
    Jan 5, 2006
  3. Replies:
    6
    Views:
    358
  4. Replies:
    1
    Views:
    383
    myork
    May 23, 2007
  5. Replies:
    1
    Views:
    371
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page