Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > object indexing and item assignment

Reply
Thread Tools

object indexing and item assignment

 
 
King
Guest
Posts: n/a
 
      11-13-2009
class MyFloat(object):
def __init__(self, value=0.):
self.value = value

def set(self, value):
self.value = value

def get(self):
return self.value

class MyColor(object):
def __init__(self, value=(0,0,0)):
self.value = (MyFloat(value[0]),
MyFloat(value[1]),
MyFloat(value[2]))

def set(self, value):
self.value[0].set(value[0])
self.value[1].set(value[1])
self.value[2].set(value[2])

def get(self):
return (self.value[0].get(),
self.value[1].get(),
self.value[2].get())

col = MyColor()
col[0].set(0.5) # 'MyColor' object does not support indexing
col[0] = 0.5 # 'MyColor' object does not support item assignment


The last two lines of the script produce errors. (written as
comments). I know it won't work as I am expecting. One solution I can
think of is to rewrite MyFloat and MyColor by sub classing default
python types "float and "tuple". Is this the only solution?

Prashant

Python 2.6.2
Win XP 32
 
Reply With Quote
 
 
 
 
Benjamin Kaplan
Guest
Posts: n/a
 
      11-13-2009
On Fri, Nov 13, 2009 at 7:57 AM, King <(E-Mail Removed)> wrote:
> class MyFloat(object):
> * *def __init__(self, value=0.):
> * * * *self.value = value
>
> * *def set(self, value):
> * * * *self.value = value
>
> * *def get(self):
> * * * *return self.value
>
> class MyColor(object):
> * *def __init__(self, value=(0,0,0)):
> * * * *self.value = (MyFloat(value[0]),
> * * * * * * * * * * * *MyFloat(value[1]),
> * * * * * * * * * * * *MyFloat(value[2]))
>
> * *def set(self, value):
> * * * *self.value[0].set(value[0])
> * * * *self.value[1].set(value[1])
> * * * *self.value[2].set(value[2])
>
> * *def get(self):
> * * * *return (self.value[0].get(),
> * * * * * * * *self.value[1].get(),
> * * * * * * * *self.value[2].get())
>
> col = MyColor()
> col[0].set(0.5) # 'MyColor' object does not support indexing
> col[0] = 0.5 # 'MyColor' object does not support item assignment
>
>
> The last two lines of the script produce errors. (written as
> comments). I know it won't work as I am expecting. One solution I can
> think of is to rewrite MyFloat and MyColor by sub classing default
> python types "float and "tuple". Is this the only solution?
>
> Prashant
>
> Python 2.6.2
> Win XP 32
> --


In order to support indexing and item assignment, implement the
__getitem__ and __setitem__ methods.


def __getitem__(self, index) :
return self.value[index]

def __setitem__(self, index, value) :
self.value[index].set(value)



> http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic indexing (multi-dimensional-indexing) (probably my most important/valuable posting up to this date) Skybuck Flying C Programming 30 09-18-2011 11:29 PM
Indexing services under Windows XP SP2 - Can I disable MS Indexing Service to hasten Google's OR does Google Desktop uses this MS Indexing Service? ricardodefaria Computer Support 6 08-05-2007 04:14 AM
How much slower is dict indexing vs. list indexing? Emin Python 4 01-12-2007 02:40 PM
Indexing PDF's using MS Indexing Service C ASP General 3 10-17-2003 05:47 PM
Indexing PDF Files using MS Indexing Service C ASP .Net 0 10-17-2003 04:27 PM



Advertisments