Discuss: Are 'one liners' pythonic? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Discuss: Are 'one liners' pythonic?

While impressive, technically speaking, does compressing something complicated into a single short line of code serve any purpose? . . . other than 'showing off' your skills? Surely, it makes your code harder to understand/maintain for others, and that's a 'bad' thing right?

8th May 2017, 11:38 AM
Lium Goon
Lium Goon - avatar
5 Answers
+ 4
Personally, I love them. Although very True - they might be hard to maintain. I rather use them to show off and to better myself. It is useful to practice them for lambdas and list comprehensions, though.
8th May 2017, 3:51 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 4
Well, PEP 8 says to limit line length to 79 chars and if you don't adhere to Guido-given rules, your code cannot be pythonic by definition. Also, don't use reduce, Guido hates that and you will burn in hell. But kidding aside, that depends on the circumstances. I'd definitely consider using list comprehensions one of the more pythonic things one can do and sometimes it's worth not to split a longer expression into two. If that one liner took you more than 5 minutes to write, chances are it's not pythonic.
9th May 2017, 10:50 AM
Tob
Tob - avatar
+ 1
It pretty much serves nothing... One liners are just made for fun, not really any other purpose
8th May 2017, 12:43 PM
Amaras A
Amaras A - avatar
0
Not a problem that developers have time to worry about... getting the code to function is the goal, pythonic or not pythonic
27th May 2017, 6:57 AM
Roger
0
Actually, when it is pythonic, it is more maintainable and reusable. You can see Raymond Hettinger's conference at PyCon 2015: beyond PEP8. It shows the power of pythonicness.
28th May 2017, 8:07 PM
Amaras A
Amaras A - avatar