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?
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.
+ 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.
+ 1
It pretty much serves nothing... One liners are just made for fun, not really any other purpose
0
Not a problem that developers have time to worry about... getting the code to function is the goal, pythonic or not pythonic
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.