Evaluation of Truth Curiosity

J

James Stroud

Hello All,

I'm curious, in

py> 0 | (1 == 1)
1
py> False | (1 == 1)
True

What is the logic of the former expression not evaluating to True (or
why the latter not 1?)? Is there some logic that necessitates the first
operand's dictating the result of the evaluation? Or is this an artefact
of the CPython implementation?

James
 
M

MonkeeSage

James said:
What is the logic of the former expression not evaluating to True (or
why the latter not 1?)? Is there some logic that necessitates the first
operand's dictating the result of the evaluation? Or is this an artefact
of the CPython implementation?

If I understand correctly, OR'ing an int and a bool (your first
example) returns an int by coercing the bool to an int; and OR'ing two
bools (your second example) returns a bool.

Regards,
Jordan
 
F

Fredrik Lundh

James said:
I'm curious, in

py> 0 | (1 == 1)
1
py> False | (1 == 1)
True

What is the logic of the former expression not evaluating to True (or
why the latter not 1?)? Is there some logic that necessitates the first
operand's dictating the result of the evaluation? Or is this an artefact
of the CPython implementation?

looks like you're confusing binary or ("|") with logical or ("or").

binary or on booleans are only defined for boolean | boolean; for any
other combination, the usual coercion rules apply.

</F>
 
C

Christophe

James Stroud a écrit :
Hello All,

I'm curious, in

py> 0 | (1 == 1)
1
py> False | (1 == 1)
True

What is the logic of the former expression not evaluating to True (or
why the latter not 1?)? Is there some logic that necessitates the first
operand's dictating the result of the evaluation? Or is this an artefact
of the CPython implementation?

James

| is the binary or operator, not the truth value operator. Also, True ==
1 and False == 0. Always use the "or" and "and" keywords for truth
operations.
 
J

James Stroud

Everyone said:
> [something intelligent]

Ah, clarity. My confusion can undoubtedly be traced to a non-existent
formal training in computer programming.

Thank you.
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top