r/Python • u/pylenin • Jan 20 '19
Code golfing with Fizzbuzz (Discover a shorter way to write python if else loop)
https://youtu.be/1khdzmUHhC41
u/neophysik Jan 20 '19
Really the fundamental issue is the mentality that you'd want to produce terse code. Terse is only good when solving for complex.. if you apply terse to something simple like an if/else you're you're adding complexity not removing it.
The pythonic way to do one liners/shorthand is through lambas, list comprehensions, map, reduce and iterators (there a more if you go beyond the standard library). But they can be abused if you use them to often or for complex statements. Think about it this way. If you have 50 lines in a function you should think about how to make it more terse and break it up. If you have 5-10 lines chances are you're just adding complexity if you try to optimize them for line and character count.
That said I've often replaced 10 lines with a one line lambda after I've worked it out. That's good practice.. write it out get it working then refine it for readability and execution speed.
-1
Jan 20 '19 edited Jan 20 '19
Post crap like that in /r/codegolf That it's implemented in Python does not make it topical here.
2
u/neophysik Jan 20 '19
I love the idea of having public code challenges.. very fun!
I feel compelled to point out you're learning bad practices/habits . In python we want verbose (yet efficient) code. It's meant to be readable and easy to inherit. Unlike in other languages (C++, Java, etc), where short code snippits make it more readable.