Override 'and' and 'or'
Bruno Desthuilliers
bruno.42.desthuilliers at wtf.websiteburo.oops.com
Sun Oct 7 10:19:17 EDT 2007
Dekker a écrit :
> 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?
http://docs.python.org/ref/customization.html
"""
__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.
"""
Not that in Python, 'and' don't yield bools:
>>> "aa" and "bb"
'bb'
>>> "aa" and None # yields None
>>> "aa" and 0
0
>>> "aa" or "bb"
'aa'
>>> 0 or "bb"
'bb'
>>> None or "bb"
'bb'
>>> "aa" or 0
'aa'
>>> "aa" or None
'aa'
>>>
HTH
More information about the Python-list
mailing list