Subclassing list, what special methods do this?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Jun 13 20:35:36 EDT 2008


En Fri, 13 Jun 2008 15:38:15 -0300, Mike Kent <mrmakent at cox.net> escribió:

> For Python 2.5 and new-style classes, what special method is called
> for mylist[2:4] = seq and for del mylist[2:4] (given that mylist is a
> list, and seq is some sequence)?
>
> I'm trying to subclass list, and I'm having trouble determining what
> special methods I have to override in my class for the above two
> operations.  From my testing, it seems to be __setslice__ for both,
> but the docs say __setslice__ and brethren are deprecated.  I would
> have thought that __setitem__ and __delitem__ would be what was
> called, but again, my testing says otherwise.

__setslice__ and __delslice__, *and* __setitem__/__delitem__ for extended  
slice notation.
The first two are deprecated, but since the list type implements them you  
have to do the same, because the interpreter invokes those methods when  
they exist, even if found in a base class.

-- 
Gabriel Genellina




More information about the Python-list mailing list