Pythonic/idiomatic?

Mark Wooding mdw at distorted.org.uk
Tue Nov 9 10:49:32 CET 2010


Seebs <usenet-nospam at seebs.net> writes:

> 	' '.join([x for x in target_cflags.split() if re.match('^-[DIiU]', x)])
>
> This appears to do the same thing, but is it an idiomatic use of list
> comprehensions, or should I be breaking it out into more bits?

It looks OK to me.  You say (elsewhere in the thread) that you're stuck
with 2.4 compatibility.  I'd personally avoid the regexp, though, and
write this (actually tested with Python 2.4!):

        ' '.join(i for i in target_cflags.split()
                   for p in 'DIiU' if i.startswith('-' + p))

> You will note that of course, I have carefully made it a one-liner so I
> don't have to worry about indentation*.

I failed at that.  You have to put up with my indentation.

-- [mdw]



More information about the Python-list mailing list