Can I make these getters/setters?

Michael George Lerner mglerner at gmail.com
Wed Sep 30 04:48:51 CEST 2009


Hi,

As part of my GUI, I have lots of fields that people can fill in,
defined like this:

        self.selection =  Pmw.EntryField(group.interior(),
                                        labelpos='w',
                                        label_text='Selection to use:
',
                                        value='(polymer)',
                                        )

I then use self.selection.get_value() and self.selection.set_value(),
and those two functions are the only ways in which I care about
self.selection. I've never really used properties, getters or setters
before. I tried this, but it didn't work:

def __init__(self):
        self._selection =  Pmw.EntryField(group.interior(),
                                        labelpos='w',
                                        label_text='Selection to use:
',
                                        value='(polymer)',
                                        )
        self.selection = property(self._selection.get_value
(),self._selection.set_value())

Of course, I really have ~40 things that I'd like to do this for, not
just one, so I'd like to find a fairly concise syntax.

In case it helps, here's a complete example of me failing. I'd like it
to print out "2" instead of "<property object at 0xe763f0>"

class Foo(object):
    def __init__(self,val):
        self._val = val
    def get(self):
        return self._val
    def set(self,val):
        self._val = val
class Bar(object):
    def __init__(self):
        self._v = Foo(2)
        self.v = property(self._v.get,self._v.set)
b = Bar()
print b.v





More information about the Python-list mailing list