Inheritting Built In Types

D

Digital Logic

I am attempting build an object which inherits from the built in list
object. Essentially I need to do something every time data is added or
changed in a list. I have all the over ridding functions working
excepting for the functions that over ride the "set slice"
functionalitity. For example:

x[1:3] = [6,7]

I consulted the reference manual:
http://docs.python.org/ref/sequence-methods.html

It states that there is a __setslice__ method which is depricated since
release 2.0. The depricated function works, but I do not want to
implement ontop of functionality that is marked to be removed. "If no
__setslice__() is found a slice object is created, and passed to
__setitem__()". I executed the below code sample to check this
behavior.

class newlist(list):
def __setitem__(self, i, data):
if isinstance(data, slice):
print "Received Slice Object"
list.__setitem(self, i, data)

if __name__ == "__main__":
x = newlist([1,2,3,4,5])
x[1:3] = [6,7]
print x

On a Windows XP machine wiht Python 2.3.5 and a Linux server with
Python 2.4.2 I receive the following output:
[1, 6, 7, 4, 5]
My print statement never gets executed.

Am I checking for the slice object incorrectly? That's the only thing
I can think of.

-Mark
 
F

Farshid Lashkari

Digital said:
Am I checking for the slice object incorrectly? That's the only thing
I can think of.

Yes. The slice object is the "i" variable in your code, not the "data"
variable.

-Farshid
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top