What is the meaning of the "if 0:" in Python?

Viewed 74

I found this piece of code:

if 0:
    somevariable = 1.0
    othervariable = 1.0

As far as I understand, 0 is always "FALSE", therefore the code never enters in the if and assigns the value to "somevariable" and "othervariable"

Does this actually make sense?

1 Answers

"falsy" values like 0 or '' evaluate to false for convenience and should not be used in the way described in your question. For example,

foo = 0
def bar():
    return foo + 1

if not foo:
    # I know that bar was not called
else:
    # I know bar was called

There is no need to check if foo == 0. https://www.geeksforgeeks.org/truthy-vs-falsy-values-in-python/ shows all the possible types that can achieve this effect.

Hope this helps!

Related