if A and Some_Function (4 ):
print 'I knew it was True'
else:
print 'I''ll never print this'
</Python>
<Output>
Ive been here 4
I knew it was True
</Output
I was expected that the function would not be called,
because A is True.
No, the function HAS to be called "because A is True"... the
situation depends on the conjunction...
"and" can only be short-circuited when it is known that one side is
False. "if False and anything:" is ALWAYS false.
"or" can be short-circuited once it is known that one side is True.
In Ada, there is actually syntax to force short-circuit evaluation
-- otherwise the compiler code optimization is free to evaluate the
sides in any order...
if this and that then...
vs
if this and then that then...
if this or that then
vs
if this or else that then
<quote>
or_test ::= and_test | or_test "or" and_test
</quote
Can you imagine, while I'm not a programmer, just a human,
that I don't understand one bit of this line.
BNF (Backus-Naur Form)... It's a notation (language) for defining
the syntax of programming languages. You need to look at the definition
for an and_test to complete the mix...
What that says is that an "or_test" is either:
an "and_test"
or it is an "or_test" separated from an "and_test" by the literal word
"or"
A recursive definition, you'll note.
I suspect that "and_test" probably has something like (I'm not
looking it up)
and_test ::= boolean | and_test "and" or_test
(since an or_test can be a naked and_test, and that and_test could be a
naked boolean, the syntax supports boolean "and" boolean, along with
and_test "and" and_test)
Non-zero integers are "True" so the evaluation is
A is 4 (non-zero, true) thereby the "and" must evaluate the other
side to determine if the whole expression is true... For simplicity,
Python just "returns" the value of B, which is 5 (non-zero, true).
Bitwise... 0100 & 0101 => 0100
A is 4 (non-zero, true), therefore there is no need to look at be,
as no matter what value is in B, the expression is going to be "true" --
for simplicity, return the value of A.
Bitwise... 0100 | 0101 => 0101
So if I use the bitwise operation on integers,
"and" changes into (bitwise) "or" and vise versa.
No it doesn't -- you chose integers that mask the real behavior...
There is one bit in common (ignore common 0 bits), and one bit that
differs.
With 6 (0110) and 5 (0101) there are one bit in common, and two bits
that differ
"and" returned b
"or" returned a
& returned only the set (1) bits common to both, clear (0) bits for
others
| returned only the clear (0) bits common to both, set (1) bits for others
"and", "or" are concerned only with "truthness" -- which is
basically defined as: true is non-zero, non-empty, non-None, false is 0,
empty, None... They return (as the truth value) the exact source value
that determined the result of the operation.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/