def __bool__(self)return true def __len__(self)return tries __bool__ first tries __len__ first truth(if xprint('yes!'yesif neither truth method is definedthe object is vacuously considered true (though any potential implications for more metaphysically inclined readers are strictly coincidental)class truthpass truth(bool(xtrue at least that' the truth in these examples won' generate exceptions in xbut some of their results there may look bit odd (and trigger an existential crisis or twounless you read the next section boolean methods in python alasit' not nearly as dramatic as billed--python users simply use __nonzero__ instead of __bool__ in all of the preceding section' code python renamed the __nonzero__ method to __bool__but boolean tests work the same otherwiseboth and use __len__ as fallback subtlyif you don' use the namethe first test in the prior section will work the same for you anyhowbut only because __bool__ is not recognized as special method name in xand objects are considered true by defaultto witness this version difference liveyou need to return falsec:\codec:\python \python class cdef __bool__(self)print('in bool'return false (bool(xin bool false if xprint( in bool this works as advertised in in xthough__bool__ is ignored and the object is always considered true by default operator overloading