On Monday, January 20, 2014 4:32:40 PM UTC+5:30, Piet van Oostrum wrote: > "Rhodri James" writes:
> > On Sat, 18 Jan 2014 16:00:45 -0000, Jussi Piitulainen wrote: > [...] > >> I would write that on three lines anyway, properly indented: > >> [ somefunc(mn,day,wd,name) > >> for (then, name) in mylist > >> let (_,mn,dy,_,_,_,wd,_,_) = localtime(then) ] > >> It could be made to use existing keywords: > >> [ somefunc(mn,day,wd,name) > >> for (then, name) in mylist > >> with localtime(then) as (_,mn,dy,_,_,_,wd,_,_) ] > > Better, in that it's readable. It's still storing up trouble, though. > > Seriously, what's inelegant about this? > > def meaningful_name(then, name): > > _,mn,dy,_,_,_,wd,_,_ = localtime(then) > > return somefunc(mn, dy, wd, name) > > ... > > [meaningful_name(then, name) for (then, name) in mylist] > > I assume there's some good reason you didn't want somefunc() to do the > > localtime() itself? > Actually in my use case somefunc(mn,day,wd,name) was an expression, not > really a function call. I just presented it like that to make it a more > generalised abstract case. So in my case that would introduce an > additional function outside of the comprehension. Nice! Thanks One of things that people who decry comprehensions (and in general hi-level constructs) dont understand is this: One fundamental characteristic of increasing hi-level-ness of a programming language is increased *freedom-in-naming* structures. The 'structures' could be expressions, statements, or whatever is the thing of the programming language. A couple of examples will illustrate: 1. In most (imperative) programming languages when a computation is described as a function it must willy-nilly be named. In lambda-calculus thats an option. 2. In any hi-level programming language, if we have an expression like sqrt(b*b - 4*a*c) we can choose to 'chunk it out' in a variety of ways: 1. t = b*b - 4*a*c sqrt(t) 2. t1 = b*b t2 = 4*a*c t3 = t1 -t2 sqrt(t3) Or the original: sqrt(b*b - 4*a*c) The assembly language programmer has no such choice. He has to write (the equivalent of) t1 = b*b t2 = 4*a t3 = t2*c t4 = t1 - t3 sqrt(t4) Compared to a comprehension, a loop is in assembly-language category since the list being 'comprehended' is compelled to be named. And BTW thanks for the singleton list trick: Sure 'v=e' is way neater than 'for v in [e]' but the for is better than nothing -- https://mail.python.org/mailman/listinfo/python-list