473,396 Members | 1,836 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

__setslice__ and classes derived from list

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
3 2082
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: | last post by:
I have this class ------------- class Component { /*class*/ Data *d; /*class*/ Draw *a; }; ------------- from "Component" derive classes like "TextBox", "Button", "Label", "ComboBox" etc from...
4
by: Xavier | last post by:
Hi, I have a question, in a "dreaded diamond" situation, regarding the following code: ---- Begin code #include <iostream> using namespace std;
1
by: Steven T. Hatton | last post by:
I started working on something I had failed to understand several months ago. The original discussion on c.l.c++-m has this message ID: news://Message-ID:...
9
by: Christian Christmann | last post by:
Hi, I've a class Handler which contains a STL list std::list<Abstract*> mAbstract; which is storing elements of the abstract class Abstract. Further this class has a getElement function to...
5
by: Nathan Bullock | last post by:
Hi, I have a base class, say Base and there are two classes, say Class1 and Class2 which are derived from Base. Is there any way for me, say from a static method in Base, to get a list of all...
3
by: craig | last post by:
Given two existing but different classes OldA and OldB (that can not be made to derive from any new base class); is there a way to make them both "observer" objects so that they can be put in one...
2
by: Zytan | last post by:
I know that WebRequest.GetResponse can throw WebException from internet tutorials. However in the MSDN docs: http://msdn2.microsoft.com/en-us/library/system.net.webrequest.getresponse.aspx It...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
7
by: ademirzanetti | last post by:
Hi there !!! I would like to listen your opinions about inherit from a STL class like list. For example, do you think it is a good approach if I inherit from list to create something like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.