Re: Property setter and lambda question

2011-07-11 Thread Anthony Kong
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

Re: Property setter and lambda question

2011-07-11 Thread Ian Kelly
On Mon, Jul 11, 2011 at 11:21 AM, Anthony Kong 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 =

Re: Property setter and lambda question

2011-07-11 Thread Ian Kelly
On Mon, Jul 11, 2011 at 10:53 AM, Anthony Kong 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 t

Re: Property setter and lambda question

2011-07-11 Thread John Posner
On 2:59 PM, Anthony Kong wrote: > 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 >

Re: Property setter and lambda question

2011-07-11 Thread Anthony Kong
> > 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

Re: Property setter and lambda question

2011-07-11 Thread Thomas Jollans
# On 07/11/2011 06:53 PM, Anthony Kong wrote: # But decorator! Of course! Thanks for reminding me this. # # In your example, where does '@not_here' come from? (Sorry, this syntax # is new to me) class A(object): def __init__(self): self.not_here = 1 @property def not_here(se

Re: Property setter and lambda question

2011-07-11 Thread Anthony Kong
Good point! Need to get my terminology right. Thanks On Tue, Jul 12, 2011 at 2:43 AM, Ian Kelly wrote: > On Mon, Jul 11, 2011 at 9:54 AM, Anthony Kong > wrote: > > Hi, all, > > This question is in the same context of my two earlier questions. This > > question was raised by some python beginner

Re: Property setter and lambda question

2011-07-11 Thread Anthony Kong
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 o

Re: Property setter and lambda question

2011-07-11 Thread Ian Kelly
On Mon, Jul 11, 2011 at 9:54 AM, Anthony Kong wrote: > 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 t

Re: Property setter and lambda question

2011-07-11 Thread Thomas Jollans
On 07/11/2011 05:54 PM, Anthony Kong wrote: > 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 de

Property setter and lambda question

2011-07-11 Thread Anthony Kong
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):