In python it is valid to make a construction like:
def a(): return 0 if a: print "Function object was considered True" else: print "Function object was considered False"
I wish to ask what is the logic that a function pointer is evaluated to True.
Why was this kind of construction inserted in the language?
Advertisement
Answer
A lot of things evaluate to True
in Python. From the documentation on Boolean operators:
In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false:
False
,None
, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true.
Functions in Python, like so many things, are objects, and not empty. Thus, in a boolean context, they evaluate to True.