returning True, False or None

N

nghoffma

Is it cheating to use a Set?

py>>def doit(thelist):
.... s = sets.Set(thelist)
.... 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
 
N

nghoffma

sorry, that should have been:

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

Fahri Basegmez

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
 
M

Michael Spencer

Fahri said:
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
 
F

Fahri Basegmez

Michael Spencer said:
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
 
M

Michael Spencer

Fahri said:
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

this may not be intentional...

Michael
 
F

Fahri Basegmez

Mick Krippendorf said:
Fahri said:
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
 
B

Brian van den Broek

Fahri Basegmez said unto the world upon 2005-02-04 23:14:
Fahri said:
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.
Likewsie, and returns the first if it evaluates to False, otherwise it
returns the second.

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
 
C

Christos TZOTZIOY Georgiou

[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 :)
 
A

Alex Martelli

Brian van den Broek said:
...
These don't do what the OP desired.

Ah, you're right, True should take precedence, point 2 of the specs.


OK, let's take advantage of the fact that None < False < True:

return max(lst)


This fails when lst is empty (the specs presumably imply a None should
be returned then). More robust:

return max(lst or [None])


Alex
 
S

Steve Holden

Christos said:
[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 :)

That wasn't a *type*, it was a *thinko*

regards
Steve
 
S

Steve Holden

Steve said:
Christos said:
[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 :)


That wasn't a *type*, it was a *thinko*

regards
Steve

Sheesh, now I even make typos typing about typos ...

giving-up-on-the-spill-chocker-ly y'rs - steve
 
F

Francis Girard

I think it is evil to do something "at your own risk" ; I will certainly not
embark some roller coaster at my own risk.

I also think it is evil to scan the whole list (as "max" ought to do) when
only scanning the first few elements would suffice most of the time.

Regards

Francis Girard

Le vendredi 4 Février 2005 21:13, Steven Bethard a écrit :
Fredrik said:
too clever. boolean > None isn't guaranteed by the language
specification:

Yup. I thought about mentioning that for anyone who wasn't involved in
the previous thread discussing this behavior, but I was too lazy. ;)
Thanks for pointing it out again.

This implementation detail was added in Python 2.1a1, with the following
note[1]:

"The outcome of comparing non-numeric objects of different types is
not defined by the language, other than that it's arbitrary but
consistent (see the Reference Manual). An implementation detail changed
in 2.1a1 such that None now compares less than any other object. Code
relying on this new behavior (like code that relied on the previous
behavior) does so at its own risk."

Steve

[1] http://www.python.org/2.1/NEWS.txt
 
M

Matteo Dell'Amico

nghoffma said:
sorry, that should have been:

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

Since a function that doesn't return is equivalent to one that returns
None, you can write it as:
.... s = set(lst) - set([None])
.... if s: return max(s)

that looks to me as the most elegant so far, but this is just because
it's mine :)

You can also filter out Nones with a list/generator comprehension, but
sets are just more elegant...
 
S

Steven Bethard

Matteo said:
Since a function that doesn't return is equivalent to one that returns
None, you can write it as:
... s = set(lst) - set([None])
... if s: return max(s)

that looks to me as the most elegant so far, but this is just because
it's mine :)

Cool. I prefer to be explicit about returns (e.g. not counting on the
automatic return None), and I'd rather not create the unnecessary None
set, so I would probably write this like:

py> def f(lst):
.... s = set(lst)
.... s.discard(None)
.... if s:
.... return max(s)
.... else:
.... return None
....

But it's definitely a very elegant solution. Thanks!

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top