New Ways to Be Told That Your Python Code Is Bad
nickdrozd.github.io
New Ways to Be Told That Your Python Code Is Bad
1–10 of 262 posts
Re: New Ways to Be Told That Your Python Code Is Bad
#2Re: New Ways to Be Told That Your Python Code Is Bad
#3I'd find that pretty annoying.
Consider a while loop in a thread:
while not quit_thread_requested:
# do threaded taskRe: New Ways to Be Told That Your Python Code Is Bad
#4Not when it's at the cost of readability. The example "better" code fails my readability test horribly. I'd gladly take C's ternary operator over this monstrosity:
> x = 4 if condition() else 5
Re: New Ways to Be Told That Your Python Code Is Bad
#5Re: New Ways to Be Told That Your Python Code Is Bad
#6> less code is better than more code Not when it's at the cost of readability. The example "better" code fails my readability test horribly. I'd gladly take C's ternary operator over this monstrosity: > x = 4 if condition() else 5
Re: New Ways to Be Told That Your Python Code Is Bad
#7 print("yes") if random.choice([True, False]) else print("no")
Does this do the right thing? I was pleasantly surprised to find that this is indeed lazily evaluated, but that's not at all intuitive: first because `print("yes")` comes before the conditional (note that the Lisp example had the conditional first), and second because not all popular languages work like that.There are lots of examples where ternary makes things obviously harder to read, such as when at least two of the three expressions are non-trivial. Which one would you rather read?
if some_complex_condition(using, four, different, parameters):
do_a_thing(now, using, five, different, parameters)
else:
do_another_thing(with, three, parameters)
or do_a_thing(now, using, five, different, parameters) if some_complex_condition(using, four, different, parameters) else do_another_thing(having, three, parameters)
(or do_a_thing(now, using, five, different, parameters) if some_complex_condition(
using, four, different, parameters
) else do_another_thing(having, three, parameters)
after Black.)Re: New Ways to Be Told That Your Python Code Is Bad
#8> less code is better than more code Not when it's at the cost of readability. The example "better" code fails my readability test horribly. I'd gladly take C's ternary operator over this monstrosity: > x = 4 if condition() else 5
Exactly. I pretty much love everything about Python except its ternary operator. I also prefer C’s.
Re: New Ways to Be Told That Your Python Code Is Bad
#9This is one of the first things that I add to a new project, before I write one line of code: https://github.com/realm/SwiftLint
I write about it here: https://littlegreenviper.com/miscellany/swiftwater/swiftlint...
Re: New Ways to Be Told That Your Python Code Is Bad
#10Sure, once you cherry-pick the most trivial imaginable example ternary expressions are easy enough to read. First counter-example which came to mind: print("yes") if random.choice([True, False]) else print("no") Does this do the right thing? I was pleasantly surprised to find that this is indeed lazily evaluated, but that's not at all intuitive: first because `print("yes")` comes before the conditional (note that the…