Pythonic/idiomatic?

geremy condra debatem1 at gmail.com
Tue Nov 9 01:11:47 CET 2010


On Mon, Nov 8, 2010 at 6:32 PM, Seebs <usenet-nospam at seebs.net> wrote:
> I have an existing hunk of Makefile code:
>        CPPFLAGS = "$(filter -D* -I* -i* -U*,$(TARGET_CFLAGS))"
> For those not familiar with GNU makeisms, this means "assemble a string
> which consists of all the words in $(TARGET_CFLAGS) which start with one
> of -D, -I, -i, or -U".  So if you give it
>        foo -Ibar baz
> it'll say
>        -Ibar
>
> I have a similar situation in a Python context, and I am wondering
> whether this is an idiomatic spelling:
>
>        ' '.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?
>
> You will note that of course, I have carefully made it a one-liner so I
> don't have to worry about indentation*.
>
> -s
> [*] Kidding, I just thought this seemed like a pretty clear expression.

I believe this is correct, but I may be wrong. Try and see.

CPPFLAGS = ' '.join(filter(lambda x: x.startswith(('-D', '-I', '-i',
'-U')), cflags))

Geremy Condra



More information about the Python-list mailing list