Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31134

Re: __setitem__ without position

Date 2012-10-11 17:32 -0400
From Dave Angel <d@davea.name>
Subject Re: __setitem__ without position
References <CA+C4C6c7GBdxs+jZPGNrpauvZXP7caGpPb8Q466HD7mV09731Q@mail.gmail.com> <CA+C4C6eMKa6i4Vk4b7hc8SxyDyutoRKkP9VLt0g3UWs+v5ehTw@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2068.1349991167.27098.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: __setitem__ without position Dave Angel <d@davea.name> - 2012-10-11 17:32 -0400

csiph-web