Re: __setitem__ without position

Discussion in 'Python' started by Terry Reedy, Oct 12, 2012.

  1. Terry Reedy

    Terry Reedy Guest

    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
     
    Terry Reedy, Oct 12, 2012
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Angel

    Re: __setitem__ without position

    Dave Angel, Oct 11, 2012, in forum: Python
    Replies:
    0
    Views:
    220
    Dave Angel
    Oct 11, 2012
  2. Ian Kelly

    Re: __setitem__ without position

    Ian Kelly, Oct 11, 2012, in forum: Python
    Replies:
    0
    Views:
    147
    Ian Kelly
    Oct 11, 2012
  3. Ethan Furman

    Re: __setitem__ without position

    Ethan Furman, Oct 12, 2012, in forum: Python
    Replies:
    0
    Views:
    171
    Ethan Furman
    Oct 12, 2012
  4. Ethan Furman

    Re: __setitem__ without position

    Ethan Furman, Oct 12, 2012, in forum: Python
    Replies:
    0
    Views:
    120
    Ethan Furman
    Oct 12, 2012
  5. Peter Otten

    Re: __setitem__ without position

    Peter Otten, Oct 12, 2012, in forum: Python
    Replies:
    0
    Views:
    127
    Peter Otten
    Oct 12, 2012
Loading...

Share This Page