returning True, False or None

Michael Spencer mahs at telcopartners.com
Fri Feb 4 23:08:06 EST 2005


Fahri Basegmez wrote:
> "Michael Spencer" <mahs at telcopartners.com> wrote in message 
> news:mailman.1946.1107575241.22381.python-list at python.org...
> 
>>Fahri Basegmez wrote:
>>
>>>reduce(lambda x, y: x or y, lst)
>>>
>>>works but when I tried
>>>
>>>import operator
>>>reduce(operator.or_, lst)
>>>
>>>this did not work.  It pukes
>>>
>>>Traceback (most recent call last):
>>>  File "<interactive input>", line 1, in ?
>>>TypeError: unsupported operand type(s) for |: 'NoneType' and 'bool'
>>>
>>>Any comments?
>>>
>>>Fahri
>>>
>>>
>>
>>TypeError: unsupported operand type(s) for |: 'NoneType' and 'bool'
>>
>>operator.or_ is "|" i.e., bitwise, not logical or
>>
>>Michael
>>
> 
> 
> That explains it.  Is there a logical or we can use with reduce?
> 
> Fahri 
> 
> 
Yes, but it's not quite the same as the 'or' operator

  >>> bool.__or__(True, False)
True
  >>> bool.__or__(False, False)
False
  >>> bool.__or__(False, None)
NotImplemented
  >>>

this may not be intentional...

Michael




More information about the Python-list mailing list