473,890 Members | 1,704 Online

# returning True, False or None

I have lists containing values that are all either True, False or None,
e.g.:

[True, None, None, False]
[None, False, False, None ]
[False, True, True, True ]
etc.

For a given list:
* If all values are None, the function should return None.
* If at least one value is True, the function should return True.
* Otherwise, the function should return False.

Right now, my code looks like:

if True in lst:
return True
elif False in lst:
return False
else:
return None

This has a light code smell for me though -- can anyone see a simpler
way of writing this?

STeVe
Jul 18 '05
35 3412
Is it cheating to use a Set?

py>>def doit(thelist):
.... s = sets.Set(thelis t)
.... if s == sets.Set([None]):
.... return None
.... else:
.... return max(s)
....
py>>print doit([ True , None , None , False ] )
True
py>>print doit([ None , False , False , None ] )
False
py>>print doit([ False , True , True , True ] )
True
py>>print doit( [None, None, None, None] )
None

Jul 18 '05 #21
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 "<interacti ve input>", line 1, in ?
TypeError: unsupported operand type(s) for |: 'NoneType' and 'bool'

Fahri
Jul 18 '05 #22
sorry, that should have been:

py>>import sets
py>>def doit(thelist):
.... s = sets.Set(thelis t)
.... if s == sets.Set([None]):
.... return None
.... else:
.... return max(s - sets.Set([None]))

Jul 18 '05 #23
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 "<interacti ve input>", line 1, in ?
TypeError: unsupported operand type(s) for |: 'NoneType' and 'bool'

Fahri

TypeError: unsupported operand type(s) for |: 'NoneType' and 'bool'

operator.or_ is "|" i.e., bitwise, not logical or

Michael

Jul 18 '05 #24

"Michael Spencer" <ma**@telcopart ners.com> wrote in message
news:ma******** *************** *************** *@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 "<interacti ve input>", line 1, in ?
TypeError: unsupported operand type(s) for |: 'NoneType' and 'bool'

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
Jul 18 '05 #25
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])

Mick.

Jul 18 '05 #26
Fahri Basegmez wrote:
"Michael Spencer" <ma**@telcopart ners.com> wrote in message
news:ma******** *************** *************** *@python.org...
Fahri Basegmez wrote:
reduce(lambd a x, y: x or y, lst)

works but when I tried

import operator
reduce(opera tor.or_, lst)

this did not work. It pukes

Traceback (most recent call last):
File "<interacti ve input>", line 1, in ?
TypeError: unsupported operand type(s) for |: 'NoneType' and 'bool'

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__(Tru e, False) True bool.__or__(Fal se, False) False bool.__or__(Fal se, None) NotImplemented

this may not be intentional...

Michael

Jul 18 '05 #27

"Mick Krippendorf" <ma******@gmx.d e> wrote in message
news:36******** *****@individua l.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])

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
Jul 18 '05 #28
Fahri Basegmez said unto the world upon 2005-02-04 23:14:
"Mick Krippendorf" <ma******@gmx.d e> wrote in message
news:36******** *****@individua l.net...
Fahri Basegmez wrote:
reduce(lambd a x, y: x or y, lst)

This doesn't solve the OPs problem since

>reduce(lam bda x, y: x or y, [False, None])

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

Jul 18 '05 #29
On Fri, 04 Feb 2005 13:04:16 -0500, rumours say that Steve Holden
<st***@holdenwe b.com> might have written:

[STeVe]
For a given list:
* If all values are None, the function should return None.
* If at least one value is True, the function should return True.
* Otherwise, the function should return False.

[Stevbe]If you wanted to get clever you could write something like

for i in True, False:
if i in lst:
return i
return False

[!Steve]

You mistyped None as False in the last line. Your typos are getting worse every
day :)
--
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
Jul 18 '05 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 16 7132 by: M-a-S | last post by: Can anybody explain this: Python 2.3 (#46, Jul 29 2003, 18:54:32) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> >>> None = 3 :1: SyntaxWarning: assignment to None >>> False = 4 >>> True = 5 >>> 46 4278 by: Scott Chapman | last post by: There seems to be an inconsistency here: Python 2.3.2 (#1, Oct 3 2003, 19:04:58) on linux2 >>> 1 == True True >>> 3 == True False >>> if 1: print "true" .... 2 1582 by: Rodney Maxwell | last post by: In Python 2.4.1: >>> None = 99 SyntaxError: assignment to None >>> True = 99 >>> False = 99 >>> True == False True ----------------------- So why is 'None' special? 10 2448 by: randomtalk | last post by: hello, i have another problem i feel that i have to be missing something.. Basically, i've written a recursive function to find all the prime up to a number (lim).. here is the function: The function basically takes in a list of all the prime number found, it takes the next number to be tested for (next) and the limit it will go up to. It divide next by the list of previous prime numbers if next is not bigger than lim, count up all the... 3 6955 by: Thirsty Traveler | last post by: I have a gridview where the datasource is bound after a selection. The result of doing this is that sorting and paging do not work. I was given a sample of how to resolve this, however my attempt to explicity enable sorting failes because, unlike the sample, my sort event has a null in the datasource and I am unsure why this is so. Sample HTML code is: