One liners

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Dec 6 21:07:13 EST 2013


On Fri, 06 Dec 2013 15:54:22 -0800, Dan Stromberg wrote:

> 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[i]
    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.


-- 
Steven



More information about the Python-list mailing list