Inheritting Built In Types

Digital Logic mroddy88 at hotmail.com
Wed Aug 16 17:40:42 EDT 2006


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




More information about the Python-list mailing list