Python Glossary Русский

all Python 3operatorfunction

operator.truth

identity and logic

Returns the truth value of an object as a bool (the same as bool(a)).

Syntax

operator.truth(a)

It does exactly what bool(a) does. In practice bool is shorter and clearer; truth shows up in older code and in operation tables. filter(None, xs) already tests truthiness, so there is no need to write filter(operator.truth, xs).

Examples

import operator
print(operator.truth([1]))   # → True
print(operator.truth(0), operator.truth(''))   # → False False
print(operator.truth([0]))   # → True
print(list(filter(operator.truth, [0, 1, '', 'ok', None, []])))   # → [1, 'ok']
print(operator.truth(5) == bool(5), operator.truth is bool)   # → True False

See also

Open in the full glossarydocs.python.orgReport an inaccuracy