returning True, False or None
Brian van den Broek
bvande at po-box.mcgill.ca
Sat Feb 5 00:00:29 EST 2005
Fahri Basegmez said unto the world upon 2005-02-04 23:14:
> "Mick Krippendorf" <mad.mick at gmx.de> wrote in message
> news:36iv03F4urakqU1 at individual.net...
>
>>Fahri Basegmez wrote:
>>
>>>reduce(lambda x, y: x or y, lst)
>>
>>This doesn't solve the OPs problem since
>>
>>
>>>>>reduce(lambda x, y: x or y, [False, None])
>>
>>returns None instead of False.
>>
>>Mick.
>>
>
>
> You are right.
> I tested None or False and it worked. I assumed order did not matter for or
> operator.
>
> None or False returns False
> False or None returns None
>
> You know what they say about assumptions. Live and learn.
>
> Fahri
Hi Fahri,
I don't have a reference at hand, but you might want to check the
docs' index or do a google for short circuit python or something similar.
or works by evaluating the first value and returning it if it
evaluates to True. Otherwise it returns the second.
>>> 0 or 42
42
>>>
Likewsie, and returns the first if it evaluates to False, otherwise it
returns the second.
>>> [] and 42
[]
>>>
The idea is that the evaluation breaks out as soon as it has seen
enough to determine the result. Hence, short circuit. And, instead of
returning a Boolean, it returns the actual object flanking the
operator. Hence, the behaviour observed.
HTH,
Brian vdB
More information about the Python-list
mailing list