Property setter and lambda question

Anthony Kong anthony.hw.kong at gmail.com
Mon Jul 11 13:56:19 EDT 2011


So subclass B has no access to __not_here in A after all...

OK, in one of legacy Python I supported there are a lot of code floating
around like this. It works OK (in term of business logic and unit test).
That's probably due to luck :-)

It also uses a lot of __slot__ = ['attr_a', 'attr_b'...] in class
definitions to prevent accidental creation of new variables (due to typo for
example).

Needless to say it was first written up by programmers of static lang
background who want to enforce the java/.net behavior... (private variables,
variable declaration)


Cheers


On Tue, Jul 12, 2011 at 3:41 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:

> On Mon, Jul 11, 2011 at 11:21 AM, Anthony Kong
> <anthony.hw.kong at gmail.com> wrote:
> > 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
>
> It shouldn't.  You've still got the name __not_here used in both A and
> B, so that the B version is setting a different attribute than the A
> version (_B__not_here vs. _A__not_here).
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 

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


More information about the Python-list mailing list