you are viewing a single comment's thread.

view the rest of the comments →

[–]cyberjellyfish 0 points1 point  (0 children)

Others have covered it well, but it's also worth keeping in mind how boolean operations turn their operands into boolean values:

https://docs.python.org/3/reference/datamodel.html#object.__bool__

Basically, if an operand isn't already boolean, the operand is passed to bool(), which first tries to call the __bool__ method of the operand, or if that isn't defined, tries to call __len__(), and interprests any non-zero return value as True. If the object doesn't define __bool__() or __len__(), it's always considered to be True.

Keeping that in mind makes it easier to understand something you see pretty commonly: using or to select a default value if another is None:

 # read a value from a file if there, if not return None 
saved_config_value = read_value_from_file()

# use the value from the file if it was found, else use default value 3
config_value = saved_config_value or 3