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]
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]])
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?
Top comments (19)
Did someone say one-line Brainf*ck interpreter?
Lambda within a lambda. You deserve a medal.
lol can't claim the credit but definitely a bunch of clever tricks in there, here's another one-line Brainf*ck interpreter
Thanks for sharing. But oneliner is not that much readable. (I think)
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
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
An insane language should have an insane interpreter!
If you are:
I highly recommend opting for more readable code.
Otherwise! You get to be the bus factor of 1 for the piece of code.
Good advice!
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
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.
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
That's one useful link!
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.
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'.
I like that philosophy!
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.
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!
My one liners are almost never one liners.
They always break into something like
var = [
# expression
]
This increases readability quite a bit IMO.