[Python-ideas] More power in list comprehensions with the 'as' keyword

Bill Janssen janssen at parc.com
Thu Sep 4 04:00:43 CEST 2008

> > We already a "with Expression as Identifier" syntax that is well known
> > and used in Python: why use something different?
> >
> > [stripped for l in text.split('\n') with l.strip() as stripped if stripped != '']
> >
> > will be a much better syntax to parse and acquire for a typical
> > pythonista. ;)
> Just because it exists, doesn't mean that it's "well known and used".
> Also, don't conflate the need to handle context management (locking,
> closing files, etc.) with the false perceived need to add temporary
> assignments in list comprehensions and generator expressions.

Hmmm, there's also "import foo as bar" and "from foo import bar as
bletch".  So "as" is gaining some traction here for these purposes
(binding a value to a named symbol).

That being said, I also prefer

  [x for x in (x.strip() for x in t.split(pat)) if x]


More information about the Python-ideas mailing list