"Rhodri James" <rho...@wildebst.org.uk> writes: > On Sat, 18 Jan 2014 16:00:45 -0000, Jussi Piitulainen > <jpiit...@ling.helsinki.fi> 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. Let me summarize what my problem really was: In a comprehension you generally need local name(s), which can only be introduced with the 'for' construction. But then the name(s) must be bound to an element of a sequence/iterator. There is no way to bind the name(s) to a single object other than putting that object in a one element sequence. I was just looking for a way to avoid that. Functional programming languages have a way to do this with the 'let' or 'where' construction which is missing in Python. Thanks everybody for your thoughts on this. -- Piet van Oostrum <p...@vanoostrum.org> WWW: http://pietvanoostrum.com/ PGP key: [8DAE142BE17999C4] -- https://mail.python.org/mailman/listinfo/python-list