Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
__setitem__ without position
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Dave Angel  
View profile  
 More options Oct 11 2012, 5:32 pm
Newsgroups: comp.lang.python
From: Dave Angel <d...@davea.name>
Date: Thu, 11 Oct 2012 17:32:21 -0400
Local: Thurs, Oct 11 2012 5:32 pm
Subject: Re: __setitem__ without position
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Kelly  
View profile  
 More options Oct 11 2012, 6:44 pm
Newsgroups: comp.lang.python
From: Ian Kelly <ian.g.ke...@gmail.com>
Date: Thu, 11 Oct 2012 16:43:21 -0600
Local: Thurs, Oct 11 2012 6:43 pm
Subject: Re: __setitem__ without position
On Thu, Oct 11, 2012 at 4:13 PM, Kevin Anthony

<kevin.s.anth...@gmail.com> wrote:
> I'm not supprised... and understand why it's happening.  I'm asking how to
> get around it.

> Basically i'm asking how to override, if i can, the `=`

You cannot override assignment of local variables.  To get around it,
use slicing as Dave suggested, or use an attribute instead of a local
variable.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Terry Reedy  
View profile  
 More options Oct 11 2012, 7:48 pm
Newsgroups: comp.lang.python
From: Terry Reedy <tjre...@udel.edu>
Date: Thu, 11 Oct 2012 19:47:24 -0400
Local: Thurs, Oct 11 2012 7:47 pm
Subject: Re: __setitem__ without position
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ethan Furman  
View profile  
 More options Oct 11 2012, 7:51 pm
Newsgroups: comp.lang.python
From: Ethan Furman <et...@stoneleaf.us>
Date: Thu, 11 Oct 2012 16:23:03 -0700
Local: Thurs, Oct 11 2012 7:23 pm
Subject: Re: __setitem__ without position

Kevin Anthony wrote:
> I'm not supprised... and understand why it's happening.  I'm asking how
> to get around it.

I don't think you do understand what's happening.

What's happening is the basic application of name binding in Python:

--> C = anything

whatever C was bound to before, it no longer is, because now it is bound
to <anything>.

What you are trying to do is mutate C, not rebind it.  As Dave
suggested, you can use slice notation ([:]) or some method of C (that
you create) to do so.

> Basically i'm asking how to override, if i can, the `=`

You can't.

~Ethan~


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ethan Furman  
View profile  
 More options Oct 12 2012, 12:21 pm
Newsgroups: comp.lang.python
From: Ethan Furman <et...@stoneleaf.us>
Date: Fri, 12 Oct 2012 09:16:52 -0700
Local: Fri, Oct 12 2012 12:16 pm
Subject: Re: __setitem__ without position

Terry Reedy wrote:
> 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__.)

I'm not sure how far back it goes, but at least from 2.4 forward
__setitem__ works with slices just fine.

~Ethan~


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options Oct 12 2012, 12:42 pm
Newsgroups: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Fri, 12 Oct 2012 18:42:37 +0200
Local: Fri, Oct 12 2012 12:42 pm
Subject: Re: __setitem__ without position

Ethan Furman wrote:
> Terry Reedy wrote:
>> 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__.)

> I'm not sure how far back it goes, but at least from 2.4 forward
> __setitem__ works with slices just fine.

The __...slice__() methods are deprecated since 2.0.

http://docs.python.org/release/2.0/ref/sequence-methods.html


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »