object indexing and item assignment

Discussion in 'Python' started by King, Nov 13, 2009.

  1. King

    King Guest

    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
    King, Nov 13, 2009
    #1
    1. Advertising

  2. On Fri, Nov 13, 2009 at 7:57 AM, King <> 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
    >
    Benjamin Kaplan, Nov 13, 2009
    #2
    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. C
    Replies:
    0
    Views:
    493
  2. Emin
    Replies:
    4
    Views:
    407
    Paul McGuire
    Jan 12, 2007
  3. Skybuck Flying
    Replies:
    30
    Views:
    1,095
    Bill Reid
    Sep 19, 2011
  4. C
    Replies:
    3
    Views:
    216
    Manohar Kamath [MVP]
    Oct 17, 2003
  5. jimbo1qaz
    Replies:
    7
    Views:
    601
    Mark Lawrence
    Sep 23, 2012
Loading...

Share This Page