One liners

Chris Angelico rosuav at gmail.com
Mon Dec 9 08:59:23 EST 2013


On Tue, Dec 10, 2013 at 12:49 AM, Neil Cerutti <neilc at norwich.edu> wrote:
> names = (p.name for p in db.query_people() if p.total_purchases > 0)
> names = (n.upper() for n in names)
> names = (n for n in names if not n.startswith("Q"))
> for n in names:
>    # Finally actually do something.
>
> Coincidentally it's a nice way to break up an ugly one-liner. I
> also really like that if I choose to no longer filter out
> customers whose name begins with Q, I can just comment out that
> one line. Try doing that with the one-liner version!

# With the restriction:
for n in (p.name.upper() for n in names if not n.startswith("Q")):

# Without the restriction:
for n in (p.name.upper() for n in names ):# if not n.startswith("Q")):

You just have to use the special "comment-out-partially" operator,
which looks like "):#" and is inspired by some kind of insane smiley
with weird hair.

ChrisA
(Anyone got the cheek de-tonguer handy?)



More information about the Python-list mailing list