Re: __setitem__ without position

Discussion in 'Python' started by Dave Angel, Oct 11, 2012.

  1. Dave Angel

    Dave Angel Guest

    On 10/11/2012 04:48 PM, Kevin Anthony wrote:
    > I have a class that contains a list of items
    > I can set items using __setitem__ but if i want to set the while list, i
    > changes the variable from a myclass to a list. How can i accomblish this
    > Example
    >>>> C = myclass()
    >>>> C[0] = 57
    >>>> type(C)

    > myclass
    >>>> C = [57,58,59,60]

    This creates a list, and binds the name that used to refer to the
    myclass to now refer to the list. The myclass object will go away,
    since there are no more refs to it.

    >>>> type(C)

    > list
    >
    >

    Why is that a surprise?

    As for how to add multiple items to the existing mylist, how about:

    for index, item in enumerate([57, 50, 59, 60]) :
    C[index] = item

    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]

    BTW, your naming capitalization is backwards. Class names should begin
    with a capital, Myclass. Instances should begin with lowercase -
    myinstance

    --

    DaveA
     
    Dave Angel, Oct 11, 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. Ian Kelly

    Re: __setitem__ without position

    Ian Kelly, Oct 11, 2012, in forum: Python
    Replies:
    0
    Views:
    147
    Ian Kelly
    Oct 11, 2012
  2. Terry Reedy

    Re: __setitem__ without position

    Terry Reedy, Oct 12, 2012, in forum: Python
    Replies:
    0
    Views:
    192
    Terry Reedy
    Oct 12, 2012
  3. Ethan Furman

    Re: __setitem__ without position

    Ethan Furman, Oct 12, 2012, in forum: Python
    Replies:
    0
    Views:
    172
    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