One liners

Neil Cerutti neilc at norwich.edu
Mon Dec 9 08:49:24 EST 2013


On 2013-12-07, Dan Stromberg <drsalists at gmail.com> wrote:
> BTW, what's pipelining style?  Like bash?

I think, in Python, it might refer to composing your program of
a series of generators.

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!

>> I'm pleased to see Python getting more popular, but it feels
>> like a lot of newcomers are trying their best to turn Python
>> into Perl or something, culturally speaking.
>
>> They're probably writing code using the idioms they are used
>> to from whatever language they have come from. Newcomers
>> nearly always do this. The more newcomers you get, the less
>> Pythonic the code you're going to see from them.
>
> Nod.

That's the sound of practicality slapping purity with a fish. A
new Python programmer can generally just get her code working in
a fairly comfortable way, then possibly rewrite it once her first
few programs become horrifying years later.

I haven't found time to rewrite all of mine yet. I still have a
program I use almost every day with an __init__ that returns
invalid objects but helpfully sets self.valid to 0.

-- 
Neil Cerutti




More information about the Python-list mailing list