[PEP] matching and mismatching
Jeff Epler
jepler at unpythonic.net
Thu May 2 12:27:15 EDT 2002
On Thu, May 02, 2002 at 05:01:59PM +0000, Luke Kenneth Casson Leighton wrote:
> for x in ([1,3,2,5,9] matching lambda x:x > 2):
> print x
As another poster has observed, you can use list comprehensions for this
purpose, as long as constructing the temporary list is palatable. Of
course,
""" print non-empty lines """
for x in file matching lambda x: len(strip(x)):
print x
might avoid reading the entire file, but
for x in [line in file if len(strip(line))]:
print x
will force the construction of a list containing all the lines to be
printed.
There was a proposal to make 'generator comprehensions', with a syntax
like
for x in [yield line in file if len(strip(line))]:
print x
but it was rejected. (http://python.org/peps/pep-0289.html)
Personally, if I got a vote, I would have been +1 on at least this part
of PEP 289, but it was rejected due to limited utility and difficulty of
implementation.
Jeff
More information about the Python-list
mailing list