Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #9269 > unrolled thread

Re: Property setter and lambda question

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2011-07-11 11:35 -0600
Last post2011-07-11 11:35 -0600
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Property setter and lambda question Ian Kelly <ian.g.kelly@gmail.com> - 2011-07-11 11:35 -0600

#9269 — Re: Property setter and lambda question

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-07-11 11:35 -0600
SubjectRe: Property setter and lambda question
Message-ID<mailman.912.1310405736.1164.python-list@python.org>
On Mon, Jul 11, 2011 at 10:53 AM, Anthony Kong
<anthony.hw.kong@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)

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web