I am looking at a snippet if not self: in an answer to another question which implements __nonzero__().
This gets me wondering: apart from __nonzero__() returning False or the trivial local assignment self = None, are there other situations, in which the conditional if not self is true?
According to Python's documentation on truth value testing:
In the code you reference,
__nonzero__()is the Python 2 equivalent of Python 3's__bool__().So, an alternative to the
__bool__()method in your question could be something like:Note: None of this has anything much to do with the title of your question: "When can self == None". Equality (whether to
Noneor to anything else) is a different concept from truth value, and is defined by the__eq__()method: