__setitem__ without position

Terry Reedy tjreedy at udel.edu
Thu Oct 11 19:47:24 EDT 2012


On 10/11/2012 5:32 PM, Dave Angel wrote:

> Alternatively, you could call one of the other methods in the class.
> But since you gave us no clues, I'm shouldn't guess what it was called.
> But if I were to make such a class, I might use slicing:
>      C[:] = [57, 50, 59, 60]

In 3.x, you would write __setitem__ to recognize that the 'key' is a 
slice object rather than an int and act accordingly. (In 2.x, you would 
write __setslice__.) Actually, if you write

def __setitem__(self, key, value):
   self.somelist[key] = value

as you might have done already, you get slice getting, setting, and 
deleting for free. Try Dave's line in your code.

-- 
Terry Jan Reedy




More information about the Python-list mailing list