Can I make these getters/setters?

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Tue Sep 29 23:55:36 EDT 2009


On Tue, 29 Sep 2009 19:48:51 -0700, Michael  George Lerner wrote:

> 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())


property() only works if the property is defined in the *class*, not the 
instance. In other words, this will work:

class K(object):
    def _parrot_getter(self):
        return "Norwegian %s" % self._colour
    parrot = property(_parrot_getter)
    def __init__(self, colour="Blue"):
        self._colour = colour

But this will not:

class K(object):
    def _parrot_getter(self):
        return "Norwegian %s" % self._colour
    def __init__(self, colour="Blue"):
        self._colour = colour
        parrot = property(_parrot_getter)




-- 
Steven



More information about the Python-list mailing list