Python Glossary Русский

all Python 3operatorfunction

operator.not_

identity and logic

Logical negation: not a.

Syntax

operator.not_(a)

The result is always a bool, even when the argument is an empty list or zero. Truthiness comes from __bool__ or, if that is missing, __len__: empty collections are false. As a function it is handy in map and filter: filter(operator.not_, xs) keeps the empty strings, zeros and None. The trailing underscore is needed because not is a keyword.

Examples

import operator
print(operator.not_(False))   # → True
print(operator.not_([]))   # → True
print(operator.not_('текст'))   # → False
print(list(map(operator.not_, [0, 1, '', 'ok'])))   # → [True, False, True, False]
print(operator.not_(5), operator.inv(5))   # → False -6

See also

Open in the full glossarydocs.python.orgReport an inaccuracy