Override 'and' and 'or'

Lawrence Oluyede raims at dot.com
Sun Oct 7 10:17:33 EDT 2007


Dekker <m.aschwanden at gmail.com> wrote:
> Is it possible to override 'and' and/or 'or'? I cannot find a special
> method for it... __and__ and __rand__ and __or__ and __ror__ are for
> binary manipulation... any proposals?

If you want to customize the truth value testing you have to implement
__nonzero__

"
 __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 (see below). If a class defines neither __len__() nor
__nonzero__(), all its instances are considered true.
"

Keep in mind the relation between __len__ and __nonzero__

ps. why you need to customize such a thing?

-- 
Lawrence, oluyede.org - neropercaso.it
"It is difficult to get a man to understand 
something when his salary depends on not
understanding it" - Upton Sinclair



More information about the Python-list mailing list