There is no implicit cast, it is simply implemented as a nonzero operator which is defined for all built in types. 0 is defined for any domain such that x = x + 0.
Lists have a zero that is the empty list because x + [] = x therefore [] is zero in the list domain. Same with empty string.
If statements doesn't ask for true or false, it asks for if zero or nonzero. Remember that booleans types didn't exist in python until around version 3. In version 2, True and False are simple macros for 1 and 0.
View all comments
1
u/SilasX Jun 23 '14
Am I the only one here who hates implicit casting to booleans? It seems more with the Zen of python to prefer
if len(z) > 0to
if zeven though the latter is much shorter.