Thanks a lot! Great points.
Looks like a good linter like github.com/wemake-services/wemake-python-stylegui… will catch almost all of these problems.
Since 0 is considered a "Falsy" value, then the comparison returns 'True`.
No, that's the kind of thing JavaScript does. Most Falsy values (None, empty string/list/dict/etc) are not equal to False.
False == 0 and True == 1 because bool is a subclass of int. You can treat booleans just like those numbers, for example True + True == 2. This can be very useful.
Very well written Miguel Brito. Thanks for sharing.
enlightening article, Love it, Thanks for sharing
Great article.
I think the example for the walrus operator could be better. Something like
# We want to increment by the number if it's nonzero, otherwise we want to increment by at least 1
x = get_some_number_maybe_0()
if x:
y += x
else:
y += 1
Which can be transformed with walrus operator to:
if x := get_some_number_maybe_0():
y += x
else:
y += 1
If the example you've shown, should_run isn't necessary, you can just base the if statement on the return of the os.environ call.
I guess the above could also be done as y += max(1, get_some_number_maybe_0()). Dammit, I need to stop finding ways to optimize my examples :P
Edit: The PEP introducing the operator has some better, albeit more complicated examples python.org/dev/peps/pep-0572
I was Waaaaat in the whole post. hahahahah Thanks to Python Tutor I can understand the logic. Loved the GIFs, btw!
David Bankson
The walrus operator is only making a 3-tuple because you are putting into a tuple in the first place. Do a print statement without the extra parens. Then try type-checking the print and it will throw an error...unless you put the parens around it so it sees a tuple. The walrus operator basically starts a new operation the way I've understood it; b := 16 evaluates and prints on its own, so everything is forced into evaluating like that.