- object.__nonzero__(self)
- Called to implement truth value testing and the built-in operation bool(); should return False or True, or their integer equivalents 0 or 1. When this method is not defined, __len__() is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither __len__() nor __nonzero__(), all its instances are considered true.
bool(some_obj)
Is evaluated like this:
some_obj.__nonzero__() if hasattr(some_obj, '__nonzero__') else len(some_obj) <> 0 if hasattr(some_obj, '__len__') else True
So basically conditionals that made with premeditation not from laziness, especially when using 3rd-party libraries/frameworks. For example, when using python requests,
>>> import requests >>> res = requests.get('https://google.pl/not_exists') >>> res <Response [404]> >>> bool(res) False >>> res = requests.get('https://google.pl/') >>> res <Response [200]> >>> bool(res) True
In case of python-requests http errors are not raised by default, instead if a status code of 4XX or 5XX is returned, the __nonzero__ method returns False. In any case res is not None is always True in the above case.