Equivalent code to the bool() built-in function

R

Raymond Hettinger

Consider the following code :

# --------------------------------------
def bool_equivalent(x):
     return True if x else False

It's faster to write:

def bool_equivalent(x):
return not not x


Raymond
 
J

John Nagle

Which Python used to do once upon a time -- and still does
in a way, because bool is a subclass of int.

The bool type was added mainly to provide a type that prints
out as 'True' or 'False' rather than 1 or 0. This can be
a considerable help for debugging and keeping the conceptual
meaning of one's data clear.

This is typical for languages which backed into a "bool" type,
rather than having one designed in. The usual result is a boolean
type with numerical semantics, like
2

which ought to either generate a TypeError or be interpreted
as "or", but due to the legacy botch, does not.

Pascal got this right. (A nice feature of Pascal
was that "packed array of boolean" was a bit array).
C, which originally lacked a "bool" type, got it wrong.
So did Python. Java is in the middle, with an isolated
"boolean" type but a system that allows casts.

John Nagle
 
G

Gregory Ewing

John said:
Pascal got this right. (A nice feature of Pascal
was that "packed array of boolean" was a bit array).
C, which originally lacked a "bool" type, got it wrong.
So did Python.

If Python had had a boolean type from the beginning, it
probably wouldn't have been a subclass of int -- that was
more or less forced by backwards compatibility issues.
Java is in the middle, with an isolated
"boolean" type but a system that allows casts.

I'm not sure that's all that much different from Pascal,
where you can use ord() to turn a boolean into an int
if you want to. At least you're being explicit.
 
G

Gregory Ewing

Chris said:
Remind me some day to finish work on my "ultimate programming
language", which starts out with a clean slate and lets the programmer
define his own operators and everything.

Didn't someone already do that and call it "lisp"? :)
 
C

Chris Angelico

Unless you're sure all of a, b, c, and d are boolean values, an int
with a negative value slipping in could result in the sum equaling 1,
but more than one of the variables evaluating to True in boolean
contexts.

If they're all expressions, then you can easily guarantee that.

ChrisA
 
S

Steven D'Aprano

If they're all expressions, then you can easily guarantee that.

*raises eyebrow*


Either of these should do the job:

sum(map(bool, (a, b, c, d)))

sum(bool(x) for x in (a, b, c, d))

but I don't see how

(arbitrary expression) + (another expression) + ... + (last expression)

can have any guarantees applied. I mean, you can't even guarantee that
they won't raise an exception. Can you explain what you mean?
 
W

Westley Martínez

but I don't see how

(arbitrary expression) + (another expression) + ... + (last expression)

can have any guarantees applied. I mean, you can't even guarantee that
they won't raise an exception. Can you explain what you mean?

What Christian posted isn't something I've often done, but here's
something slightly different that exploits the same
comparisons-return-summable-values concept:

A condition with N subconditions is deemed to be satisfied if a
minimum of M of them are true. This is a general case of the boolean
Or (N = 2, M = 1) and And (N = 2, M = 2), but does not have a direct
equivalent in binary operators. You simply sum the subconditions,
compare against M, and you have your answer.

if (((port<1024) + (!ip.startswith("192.168.")) +
(greylist[ip]>time()) + (++spewcnt>10))>=3) // flag this packet as
suspicious

Contrived example as I don't recall any specifics right now, but this
will pick up any packets where three or more of the conditions are
met. Useful only in fairly specific situations, but I don't know of
any way to do this with just AND/OR/NOT that would be as clear and
simple.

Chris Angelico

Exclusive or:(False and True and False and False) or
(False and False and True and False) or
(False and False and False and True):
.... print(True)

Maybe a little silly.
 
G

Grant Edwards

Am 18.04.2011 21:58, schrieb John Nagle:

I find the behavior rather useful. It allows multi-xor tests like:

if a + b + c + d != 1:
?? ??raise ValueError("Exactly one of a, b, c or d must be true.")

I guess I never thought about it, but there isn't an 'xor' operator to
go along with 'or' and 'and'. Must not be something I need very often.
 
J

Jean-Paul Calderone

I guess I never thought about it, but there isn't an 'xor' operator to
go along with 'or' and 'and'.  Must not be something I need very often.

You also can't evaluate xor without evaluating both operands, meaning
there
is never a short-circuit; both and and or can short-circuit, though.
Also
boolean xor is the same as !=.

Jean-Paul
 
G

Gregory Ewing

Jean-Paul Calderone said:
Also boolean xor is the same as !=.

Only if you have booleans. Even without short circuiting,
a boolean xor operator could provide the service of
automatically booling things for you (is that a word?).
 

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

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top