Pythonic/idiomatic?

Seebs usenet-nospam at seebs.net
Tue Nov 9 01:34:21 CET 2010


On 2010-11-09, Ben Finney <ben+python at benfinney.id.au> wrote:
> For this purpose, there is a generator expression syntax
><URL:http://docs.python.org/reference/expressions.html#generator-expressions>,
> almost identical to a list comprehension except without the enclosing
> brackets.
>
>     ' '.join(x for x in target_cflags.split() if re.match('^-[DIiU]', x))

Ahh, handy.

I am torn very much on the generator/comprehension syntax, because on the
one hand, I really prefer to have the structure first, but on the other hand,
I can't easily figure out a way to make the syntax work for that.

> The regex is less clear for the purpose than I'd prefer. For a simple
> ???is it a member of this small set???, I'd find it more readable to use a
> simple list of the actual strings::

>     ' '.join(
>         x for x in target_cflags.split()
>         if x in ['-D', '-I', '-i', '-U'])

The regex is intentionally not anchored with a $, because I'm looking
for "starts with", not "is".

> The latter works only in Python with set literals (Python 2.7 or later).

I think we're stuck with backwards compatibility at least as far as 2.4.

No, I'm not kidding.  *sigh*

-s
-- 
Copyright 2010, all wrongs reversed.  Peter Seebach / usenet-nospam at seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.



More information about the Python-list mailing list