Looking for a list subclassing example...

Mike C. Fletcher mcfletch at rogers.com
Tue Jun 4 18:03:57 CEST 2002


Well, here's a generic list-subclass example...

class rlist( list ):
     """Sub-class of list which calls a method before any addition is 
allowed"""
     def __setslice__( self, start, stop, value ):
         """__setslice__ with value checking"""
         value = self.beforeMultipleAdd([ self.beforeAdd(item) for item 
in value ])
         return list.__setslice__( self, start, stop, value )
     def extend( self, value ):
         """extend with value checking"""
         value = self.beforeMultipleAdd([ self.beforeAdd(item) for item 
in value ])
         return list.extend( self, value )
     __iadd__ = extend
     def append( self, value ):
         """append with value checking"""
         value = self.beforeAdd( value )
         return list.append( self, value )
     def insert( self, index, value ):
         """insert with value checking"""
         value = self.beforeAdd( value )
         return list.insert( self, index, value )
     def __setitem__( self, index, value ):
         """__setitem__ with value checking"""
         value = self.beforeAdd( value )
         return list.__setitem__( self, index, value )
     def beforeAdd( self, value ):
         """Called before all attempts to add an item"""
         return value
     def beforeMultipleAdd( self, value ):
         """Called before attempts to add more than one item (beforeAdd 
has already be called for each item)"""
         return value

HTH,
Mike


Shagshag13 wrote:
> Hello,
> 
> I'm looking for a list subclassing example, and i can't find it...
> 
> Do you have one to show ?
> 
> Thanks,
> 
> s13.
> 
> 
> 


-- 
_______________________________________
   Mike C. Fletcher
   http://members.rogers.com/mcfletch/







More information about the Python-list mailing list