I did some tests:
>>> empty_recordset = self.env['res.users'] # empty recordset >>> not_empty_recordset = self.env['res.users'].search([('id', '=', 1)]) # recordset with one record >>> empty_recordset is False False >>> empty_recordset is None False >>> empty_recordset == False False >>> empty_recordset == True False >>> bool(empty_recordset) False >>> not empty_recordset True >>> if empty_recordset: # it is treated as False ... print('hello') ... >>> bool(not_empty_recordset) True >>> if not_empty_recordset: ... print('hello') ... hello >>> not not_empty_recordset False
bool()
True
False
if
not
is
==
!=
What's happening? Is a recordset processed as a boolean with ifand instructions only not? Are the other operators not overloaded?
This is the way __nonzero__:
__nonzero__
bool(); False True 0 1. , len(), , , , len(), (), .
odoo/odoo/models.py:
Odoo 10 :
def __nonzero__(self): """ Test whether ``self`` is nonempty. """ return bool(getattr(self, '_ids', True))
, . python
if object: # is turned to. if object.__nonzero__():
if object == value: #is turned to if object.__eq__(value):
:
object + value # is converted go this object.__add__(value)
.
, python .
Source: https://habr.com/ru/post/1690626/More articles:Rails Not querying entire section Where - sqlDetecting the outer extreme edge of an image and plotting it based on it - pythonWhy doesn't GCC allow me to create `inline static std :: stringstream`? - c ++ColdFusion - no function found [functionName] - cfmlFind minimum distances between groups of points in 2D (fast and not too much memory) - pythonprocessing negation in R, how can I replace a word after negation in R? - rLimitations / features of iOS storyboards for portrait and landscape views - iosCoordinate filtering based on distance from a point - pythonApache pool apache worker thread startup error: Failed to install packages: failed to install SDK: exit status 2 - pythonGet a type that implements a common interface by searching for a specific general interface parameter - reflectionAll Articles