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):

Re: Lambda question

2011-06-06 Thread Terry Reedy
On 6/6/2011 12:52 PM, Terry Reedy wrote: def group(seq, n): 'Yield from seq successive disjoint slices of length n & the remainder' if n<=0: raise ValueError('group size must be positive') for i in range(0,len(seq), n): yield seq[i:i+n] for inn,out in ( (('',1), []), # no input, no output #(('a

Re: Lambda question

2011-06-06 Thread Terry Reedy
On 6/6/2011 1:29 PM, rusi wrote: On Jun 5, 11:33 pm, Terry Reedy wrote: Let me add something not said much here about designing functions: start with both a clear and succinct definition *and* test cases. (I only started writing tests first a year ago or so.) I am still one year in the futu

Re: Lambda question

2011-06-06 Thread harrismh777
Steven D'Aprano wrote: For any non-trivial function, I usually start by writing the documentation (a docstring and doctests) first. How else do you know what the function is supposed to do if you don't have it documented? Yes. In my early years I was no different than any other hacker in terms

API design before implementation (was: Lambda question)

2011-06-06 Thread Ben Finney
Steven D'Aprano writes: > On Mon, 06 Jun 2011 12:52:31 -0400, Terry Reedy wrote: > > > Let me add something not said much here about designing functions: start > > with both a clear and succinct definition *and* test cases. (I only > > started writing tests first a year ago or so.) > > For any no

Re: Lambda question

2011-06-06 Thread Steven D'Aprano
On Mon, 06 Jun 2011 12:52:31 -0400, Terry Reedy wrote: > Let me add something not said much here about designing functions: start > with both a clear and succinct definition *and* test cases. (I only > started writing tests first a year ago or so.) For any non-trivial function, I usually start b

Re: Lambda question

2011-06-06 Thread rusi
On Jun 5, 11:33 pm, Terry Reedy wrote: > On 6/5/2011 5:31 AM, Alain Ketterlin wrote: > > >  writes: > > > f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc > > f=lambda ... statements are inferior for practical purposes to the > equivalent def f statements because the resultin

Re: Lambda question

2011-06-06 Thread Terry Reedy
On 6/6/2011 9:42 AM, jyoun...@kc.rr.com wrote: f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc Packing tail recursion into one line is bad for both understanding and refactoring. Use better names and a docstring gives def group(seq, n): 'Yield from seq successive disjoin

RE: Lambda question

2011-06-06 Thread jyoung79
> f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc > Packing tail recursion into one line is bad for both understanding and > refactoring. Use better names and a docstring gives > > def group(seq, n): >'Yield from seq successive disjoint slices of length n plus the > re

Re: Lambda question

2011-06-05 Thread Terry Reedy
On 6/5/2011 5:31 AM, Alain Ketterlin wrote: writes: f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc f=lambda ... statements are inferior for practical purposes to the equivalent def f statements because the resulting object is missing a useful name attribute and a docstr

Re: Lambda question

2011-06-05 Thread John Posner
On 2:59 PM, Ian Kelly wrote: > On Sat, Jun 4, 2011 at 12:09 PM, Chris Angelico wrote: >> Python doesn't seem to have an inbuilt function to divide strings in >> this way. At least, I can't find it (except the special case where n >> is 1, which is simply 'list(string)'). Pike allows you to use the

Re: Lambda question

2011-06-05 Thread Alain Ketterlin
writes: f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc f("Hallo Welt", 3) > ['Hal', 'lo ', 'Wel', 't'] > > http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-s > ized-chunks-in-python/312644 > > It doesn't work with a huge list, but l

Re: Lambda question

2011-06-04 Thread Vito 'ZeD' De Tullio
jyoun...@kc.rr.com wrote: > I was surfing around looking for a way to split a list into equal > sections. non-recursive, same-unreadeable (worse?) one liner alternative: def chunks(s, j): return [''.join(filter(None,c))for c in map(None,*(s[i::j]for i in range(j)))] -- By ZeD -- http:/

Re: Lambda question

2011-06-04 Thread Ian Kelly
On Sat, Jun 4, 2011 at 12:09 PM, Chris Angelico wrote: > Python doesn't seem to have an inbuilt function to divide strings in > this way. At least, I can't find it (except the special case where n > is 1, which is simply 'list(string)'). Pike allows you to use the > division operator: "Hello, worl

Re: Lambda question

2011-06-04 Thread Mel
jyoun...@kc.rr.com wrote: > I was surfing around looking for a way to split a list into equal > sections. I came upon this algorithm: > f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc f("Hallo Welt", 3) > ['Hal', 'lo ', 'Wel', 't'] > > http://stackoverflow.com/ques

Re: Lambda question

2011-06-04 Thread Chris Angelico
On Sun, Jun 5, 2011 at 3:46 AM, wrote: > It doesn't work with a huge list, but looks like it could be handy in certain > circumstances.  I'm trying to understand this code, but am totally lost.  I > know a little bit about lambda, as well as the ternary operator, but how > does this part work: >

Lambda question

2011-06-04 Thread jyoung79
I was surfing around looking for a way to split a list into equal sections. I came upon this algorithm: >>> f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc >>> f("Hallo Welt", 3) ['Hal', 'lo ', 'Wel', 't'] http://stackoverflow.com/questions/312443/how-do-you-split-a-list

Re: lambda question

2010-06-12 Thread Vincent Davis
On Fri, Jun 11, 2010 at 10:11 PM, Ian Kelly wrote: > On Fri, Jun 11, 2010 at 9:31 PM, Vincent Davis > wrote: >> Starting with an example. >> In [23]: x = [1,2,3,4,4,4,5,5,3,2,2,] >> In [24]: y = set(x) >> In [25]: y >> Out[25]: set([1, 2, 3, 4, 5]) >> In [26]: y2 = len(set(x)) >> In [27]: y2 >>

Re: lambda question

2010-06-11 Thread Ian Kelly
On Fri, Jun 11, 2010 at 9:31 PM, Vincent Davis wrote: > Starting with an example. > In [23]: x = [1,2,3,4,4,4,5,5,3,2,2,] > In [24]: y = set(x) > In [25]: y > Out[25]: set([1, 2, 3, 4, 5]) > In [26]: y2 = len(set(x)) > In [27]: y2 > Out[27]: 5 > > How would I do the above "y2 = len(set(x))" but ha

lambda question

2010-06-11 Thread Vincent Davis
Starting with an example. In [23]: x = [1,2,3,4,4,4,5,5,3,2,2,] In [24]: y = set(x) In [25]: y Out[25]: set([1, 2, 3, 4, 5]) In [26]: y2 = len(set(x)) In [27]: y2 Out[27]: 5 How would I do the above "y2 = len(set(x))" but have len(set()) in a dictionary. I know how to do .. In [30]: d = dict(s=set