Property setter and lambda question
Ian Kelly
ian.g.kelly at gmail.com
Mon Jul 11 13:35:05 EDT 2011
On Mon, Jul 11, 2011 at 10:53 AM, Anthony Kong
<anthony.hw.kong at gmail.com> wrote:
> Thanks again for your input, Thomas.
> I normally prefer
> not_here = property(lambda self: self.__get_not_here(), lambda self, v:
> self.__set_not_here(v))
> than
> not_here = property(__get_not_here, __set_not_here)
> Because it allows me to have a pair getter/setter (when there is a need for
> it). Use of lambda there is ensure derived class of A can provide their
> custom version of getter/setter.
The .setter convenience method also makes it a bit easier for derived
classes to modify getters and setters:
class Base(object):
def get_my_property(self):
return self._my_property
def set_my_property(self, value):
self._my_property = value
my_property = property(get_my_property, set_my_property)
class Derived(Base):
def set_my_property(self, value):
super(Derived, self).set_my_property(convert(value))
my_property = Base.my_property.setter(set_my_property)
More information about the Python-list
mailing list