In Python, there are several values that count as False:
>>> if False or None or 0 or 0.0 or '' or [] or {} or set():
... print 'True :)'
... else:
... print 'False :('
...
False :(
So, somewhere in my code I’m doing something like this:
some_var = other_var or 'default' # Some default value
other_var could be None or any other value, if it’s None, some_var should be filled with 'default'.
But I kept having some troubles with this, so, debugging my code I found out that other_var sometimes was 0 (or any other value that counts as False), and some_var was filled with 'default' instead of 0.
So, is there a way to accomplish what I’m trying rather than doing this?
if other_var is None:
some_var = 'default'
else:
some_var = other_var
No is a valid answer.
There is the Conditional Expression (aka the “ternary operator”) syntax:
Happy coding.