Property setter and lambda question

Anthony Kong anthony.hw.kong at gmail.com
Mon Jul 11 19:21:14 CEST 2011


>
> PS: are you sure the lambda self: self.__foo() trick works, with
> subclasses or otherwise? I haven't tested it, and I'm not saying it
> doesn't, but I have a feeling double-underscore name mangling might be a
> problem somewhere down the line?
>
>
Awesome, Thomas. The trick only works if there is only *one* leading
underscore in the *method* names.

The following example works as I expected for the derived class B.

class A(object):

    def __init__(self):
        self.__not_here = 1

    def _get_not_here(self):
        return self.__not_here

    def _set_not_here(self, v):
        print "I am called"
        self.__not_here = v

    not_here = property(lambda self: self._get_not_here(), lambda self, v:
self._set_not_here(v))

class B(A):
    def _set_not_here(self, v):
        print "version B"
        self.__not_here = v


a = A()
# print a.__not_here
print a.not_here
# a.set_not_here(10)
a.not_here = 10
print a.not_here


b = B()
print b.not_here
b.not_here = 70  # print version B
print b.not_here




-- 

Tony Kong
*blog:* www.ahwkong.com

Don’t EVER make the mistake that you can design something better than what
> you get from ruthless massively parallel trial-and-error with a feedback
> cycle. That’s giving your intelligence *much* too much credit.


- Linus Torvalds
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110712/e5c0c2a6/attachment.html>


More information about the Python-list mailing list