object indexing and item assignment

King animator333 at gmail.com
Fri Nov 13 13:57:04 CET 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



More information about the Python-list mailing list