Property setter and lambda question

Anthony Kong anthony.hw.kong at gmail.com
Mon Jul 11 17:54:18 CEST 2011


Hi, all,

This question is in the same context of my two earlier questions. This
question was raised by some python beginners, and I would like to check with
the list to ensure I provide a correct answer.

Here is a code snippet I used to demonstrate the keyword *property*:


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))
    # not_here = property(lambda self: self.__not_here, lambda self, v:
self.__not_here = v)

So the question: is it possible to use lambda expression at all for the
setter? (As in the last, commented-out line)

Python interpreter will throw an exception right there if I use the last
line ('SyntaxError: lambda cannot contain assignment'). I'd use pass a
setter method anyway.

What is your preferred solution?

-- 

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/4ddcf8cd/attachment.html>


More information about the Python-list mailing list