DEV Community

loading...

Are one liners always pythonic?

miguelmj profile image MiguelMJ ・1 min read

First of all, if you don't know what pythonic means, see this question.

I love clean code as much as anyone, but I think we all agree that there should be a limit on how much you put on a single line of code.

For example, looking at some examples on the Python wiki I came across elegant one liners like:

# Palindrome Python One-Liner
phrase.find(phrase[::-1])

# Find indices of x in a list lst
indices = [i for i in range(len(lst)) if lst[i]==x]
Enter fullscreen mode Exit fullscreen mode

but also some others that I don't find that much clean:

# Quicksort Python One-liner
lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])
Enter fullscreen mode Exit fullscreen mode

Do you have any criteria to limit your one-liners? Or don't you mind about the length of a line as long as it does the work?

Discussion (19)

pic
Editor guide
Collapse
chrisgreening profile image
Chris Greening

Did someone say one-line Brainf*ck interpreter?

( lambda ins, sys: ( ( lambda f: f( f, 0, 0, data=[0]*100000, bp=( (lambda f: f(f, [], {}, 0)[0][1])( lambda g, bs, bp, ix: ( ((bs, bp),) if ix >= len(ins) else ( (g(g, bs + [ix], bp, ix+1), )[0] if ins[ix] == '[' else ( (g(g, bs[:-1], bp, ix+1), bp.update({ix: bs[-1], bs[-1]:ix}),)[0] if ins[ix] == ']' else g(g, bs, bp, ix+1) ) ) ) ) ) ) )( lambda g, ptr, i, data, bp: ( None if i >= len(ins) else g(g, *{ '>': lambda d, p, i, brace_pairs: (p + 1, i+1,), '<': lambda d, p, i, brace_pairs: (p - 1, i+1,), '+': lambda d, p, i, brace_pairs: (p, i+1, d.__setitem__(p, (d[p]+1)%256))[:-1], '-': lambda d, p, i, brace_pairs: (p, i+1, d.__setitem__(p, (d[p]-1)%256))[:-1], '.': lambda d, p, i, brace_pairs: (p, i+1, sys.stdout.write(chr(d[p])))[:-1], ',': lambda d, p, i, brace_pairs: (p, i+1, d.__setitem__(p, ord(sys.stdin.buffer.read(1))))[:-1], '[': lambda d, p, i, brace_pairs: (p, brace_pairs[i] + 1 if not d[p] else i+1, ), ']': lambda d, p, i, brace_pairs: (p, brace_pairs[i] + 1 if d[p] else i+1, ), }[ins[i]](data, ptr, i, bp), data, bp) ) ) ) )( ''.join(i for i in open(__import__('sys').argv[1]).read() if i in {'<', '>', '+', '-', '.', ',', '[', ']'}), (__import__('sys'), __import__('sys').setrecursionlimit(100000))[0], )
Enter fullscreen mode Exit fullscreen mode
Collapse
qviper profile image
Viper

Lambda within a lambda. You deserve a medal.

Collapse
chrisgreening profile image
Chris Greening

lol can't claim the credit but definitely a bunch of clever tricks in there, here's another one-line Brainf*ck interpreter

Thread Thread
qviper profile image
Viper

Thanks for sharing. But oneliner is not that much readable. (I think)

Thread Thread
miguelmj profile image
MiguelMJ Author

That's true, but I guess for some they are like a challenge. It happens the same with esoteric languages... We could say that Python becomes an esoteric language when you apply the rule "all in one line" hahaha

Thread Thread
chrisgreening profile image
Chris Greening

Pretty recently I was writing a program that was going to be automated on a server with limited space so I crunched the program from 100 lines to like 15 awful one liners and it cut the size of the file down to like less than a kilobyte lol it was a fun challenge to see how small I could get the program and I could see people doing it for fun/proof of concept

Collapse
miguelmj profile image
MiguelMJ Author

An insane language should have an insane interpreter!

Collapse
justinkaffenberger profile image
JustinKaffenberger

If you are:

  • Maintaining a long lived product
  • Attempting to scale your teams with new talent
  • Are willing to let go of your code wizard ego

I highly recommend opting for more readable code.

Otherwise! You get to be the bus factor of 1 for the piece of code.

Collapse
miguelmj profile image
MiguelMJ Author

Good advice!

Collapse
chrisgreening profile image
Chris Greening

lol I've seen some pretty insane one-liners, I think my criteria is as long as it doesn't look ugly. Once a one-liner isn't almost immediately obvious I always break it into multiple lines

Collapse
miguelmj profile image
MiguelMJ Author

That's more or less my criteria too.
Out of curiosity, have other people read the one-liners you thought were obvious, but had to take their time to understand them?
In my case, not many people see my code 😅 so I'm not sure if that's the same for everyone. In the end, the level of experience could make vary what is obvious and what no.

Collapse
chrisgreening profile image
Chris Greening

I've kind of gotten a feel for what makes sense and what doesn't and this translates to readability for others as well. If the one-liner is longer than 79 characters that's usually a red flag and I always break my statements up as much as possible before introducing a complicated one liner. They usually end up reading more like pseudo code than anything too wild

I will admit I've gotten lazy though and written crazy one liners for throwaway programs or programs under time constraint and will return months later and have trouble reading my own code lol

Thread Thread
miguelmj profile image
MiguelMJ Author

That's one useful link!

Collapse
evanroggenkamp profile image
Evan Roggenkamp

The only people I’ve found who find one liners hard to navigate are people who aren’t already familiar with comprehensions. Which is always a fun thing to teach.

Collapse
alainvanhout profile image
Alain Van Hout

Given that that second example packs quite an awful lot on a single line, I'm pretty it runs afoul of 'sparse is better than dense'.

Collapse
miguelmj profile image
MiguelMJ Author

I like that philosophy!

Collapse
patarapolw profile image
Pacharapol Withayasakpunt

One-liner is a joke. Isn't minified Javascript also a one liner?

Python can also force one liner using semicolon. (And also break new line using back slash, IIRC.)

That aside, readability is the most important, and after that, performance.

Make mutable variables and loops if you have to. You don't need generator expressions every time, and it might confuse you.

Also, named functions might be preferable to lambdas in any programming languages. It will make testing easier.

Collapse
miguelmj profile image
MiguelMJ Author

I think that with semicolons doesn't count as one liner. Also, sometimes on liners improve readability.
However, I think you're right about not having to use always generators. Just because you can doesn't mean that you should!

Collapse
evanroggenkamp profile image
Evan Roggenkamp

My one liners are almost never one liners.

They always break into something like

var = [
# expression
]

This increases readability quite a bit IMO.