By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

__setslice__ and classes derived from list

P: n/a
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
Sep 2 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Sep 2 '05 #2

P: n/a
In article <df**********@gemini.csx.cam.ac.uk>,
Michael Hoffman <ca*******@mh391.invalid> 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
Sep 2 '05 #3

P: n/a
Dave Opstad wrote:
I'm happy to submit a feature request, once I figure out how to do it!


http://sourceforge.net/projects/python/

Follow the RFE link.
--
Michael Hoffman
Sep 2 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.