One liners

D

Dan Stromberg

Does anyone else feel like Python is being dragged too far in the direction
of long, complex, multiline one-liners? Or avoiding temporary variables
with descriptive names? Or using regex's for everything under the sun?

What happened to using classes? What happened to the beautiful emphasis on
readability? What happened to debuggability (which is always harder than
writing things in the first place)? And what happened to string methods?

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.
 
S

Steven D'Aprano

Does anyone else feel like Python is being dragged too far in the
direction of long, complex, multiline one-liners? Or avoiding temporary
variables with descriptive names? Or using regex's for everything under
the sun?

All those things are stylistic issues, not language issues. Yes, I see
far too many people trying to squeeze three lines of code into one, but
that's their choice, not the language leading them that way.

On the other hand, Python code style is influenced strongly by functional
languages like Lisp, Scheme and Haskell (despite the radically different
syntax). Python has even been described approvingly as "Lisp without the
brackets". To somebody coming from a C or Pascal procedural background,
or a Java OOP background, such functional-style code might seem too
concise and/or weird. But frankly, I think that such programmers would
write better code with a more functional approach. I refuse to apologise
for writing the one-liner:

result = [func(item) for item in sequence]

instead of four:

result = []
for i in range(len(sequence)):
item = sequence
result.append(func(item))

What happened to using classes? What happened to the beautiful emphasis
on readability? What happened to debuggability (which is always harder
than writing things in the first place)? And what happened to string
methods?

What about string methods?

As far as classes go, I find that they're nearly always overkill. Most of
the time, a handful of pre-written standard classes, like dict, list,
namedtuple and the like, get me 90% of the way to where I need to go.

The beauty of Python is that it is a multi-paradigm language. You can
write imperative, procedural, functional, OOP, or pipelining style (and
probably more). The bad thing about Python is that if you're reading
other people's code you *need* to be familiar with all those styles.

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.
 
D

Dan Stromberg

On Fri, Dec 6, 2013 at 6:07 PM, Steven D'Aprano <
All those things are stylistic issues, not language issues. Yes, I see
far too many people trying to squeeze three lines of code into one, but
that's their choice, not the language leading them that way.

Yes, stylistic, or even "cultural".

I refuse to apologise
for writing the one-liner:

result = [func(item) for item in sequence]

instead of four:

result = []
for i in range(len(sequence)):
item = sequence
result.append(func(item))

IMO, this is a time when the one liner is more clear. But if you start
trying to stretch that to extremes, it becomes worse instead of better.
What about string methods?
A lot of things people do with regex's, could be done with string methods
more clearly and concisely.

The beauty of Python is that it is a multi-paradigm language. You can
write imperative, procedural, functional, OOP, or pipelining style (and
probably more). The bad thing about Python is that if you're reading
other people's code you *need* to be familiar with all those styles.

That's fine. That's appropriate. But I imagine any of these can be done
with the intention of being more clever than clear.

BTW, what's pipelining style? Like bash?
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.
 
R

Roy Smith

Dan Stromberg said:
A lot of things people do with regex's, could be done with string methods
more clearly and concisely.

That is true. The problem is, there are a lot of things for which regex
is the right tool, but people get out of practice using them (or never
learned how) because they gravitate to string methods for most tasks.

It's like any sharp tool. When skillfully handled, they're excellent at
the tasks they were designed for. But, if you don't practice the
necessary skills, you end up just re-enacting the Black Night Sketch.
 
N

Neil Cerutti

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!

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.
 
C

Chris Angelico

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?)
 
S

Steven D'Aprano

That's fine. That's appropriate. But I imagine any of these can be
done with the intention of being more clever than clear.

BTW, what's pipelining style? Like bash?

Yes, correct. You have a data stream and you pass it through various
filters to process it.

David Beazley has some nice examples of writing pipelining code:

http://www.dabeaz.com/generators/index.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top