I definitely don’t want it to be incorrect. The only example I keep thinking of is a while loop, but that’s a bit more advanced than the current notes.
Interesting question. In Python bool built-in evaluate objects to True/False constants according to dunder __bool__ method of object.
See, for instance, this simple example:
classPositive:def__init__(self,value):self.value=valuedef__bool__(self):returnself.value>0plus_ten=Positive(10)minus_ten=Positive(-10)minus_twenty=Positive(-20)assertbool(plus_ten)==1==Trueassertbool(minus_ten)==0==Falseassert(minus_tenorplus_ten).value==10# This is actually the tricky part
# and the reason why it is not great idea to evaluate
# your expression like this
# instead ternary X if Expr else Y
assertbool(plus_tenandminus_ten)==Falseassert(plus_tenandminus_ten).value==-10assertbool(minus_tenorminus_twenty)==Falseassert(minus_tenorminus_twenty).value==-20
Note my last lines. The result of expressions is not true, yet Python returns the last of them, which is evidently False. It is OK in conditional statement, but certainly not OK, if you use return X or Y.
Python uses short-circuit evaluation, meaning, the and or or expressions return the last thing they had to evaluate. In X or Y if X is True, Y is never evaluated. Similarly, in X and Y there is no need to evaluate Y if X is already False.
So, part of my problem understanding your original post was that I had never seen assert before. I've read up on it and now I think I can make sense of this.
If I'm now understanding correctly, and & or statements seem similar to if, else, and elif statements.
I'm thinking something like this is a simple enough example.
For instance, & evaluates both operands, while && behaves similar to Python's and.
It is important to have good understanding of this, or you may run into hard to find bugs, which are not obvious when you don't know what you are looking at.
By the way, since I wrote this from my mobile, I had no opportunity to check my assertions. Be cautious 😊
We're a place where coders share, stay up-to-date and grow their careers.
I definitely don’t want it to be incorrect. The only example I keep thinking of is a while loop, but that’s a bit more advanced than the current notes.
People use or abuse this construction. It is shorter but harder to understand. Not to speak it may backfire eventually.
return x or y
instead of
return x if x else y
One can never understand such code if one thinks and or or evaluate to True/False.
As mentioned before, True/False is actually 1/0. So, are you saying we should be using
and
&or
to evaluate to 1/0?I’ve tried to find examples that don’t lead to a Boolean outcome and I’m at a loss.
I read the python docs this morning and it refers to these as Boolean operators.
Interesting question. In Python bool built-in evaluate objects to True/False constants according to dunder __bool__ method of object.
See, for instance, this simple example:
Note my last lines. The result of expressions is not true, yet Python returns the last of them, which is evidently False. It is OK in conditional statement, but certainly not OK, if you use return X or Y.
Python uses short-circuit evaluation, meaning, the and or or expressions return the last thing they had to evaluate. In X or Y if X is True, Y is never evaluated. Similarly, in X and Y there is no need to evaluate Y if X is already False.
So, part of my problem understanding your original post was that I had never seen
assert
before. I've read up on it and now I think I can make sense of this.If I'm now understanding correctly,
and
&or
statements seem similar toif
,else
, andelif
statements.I'm thinking something like this is a simple enough example.
You are quite diligent.
I am using assert as simple testing tool.
If my assertion was wrong, I would end up with Assert Exception, not posting anything.
Typically, I am using this style to prevent myself from posting incorrect information.
It never came to my mind, someone would have trouble to decipher it. My bad.
and and or are operators, similar to plus or minus.
For instance,
1+3 = 4.
Likewise,
1 and 3 = 3
1 or 3 = 1
I mean they are not evaluating to True or False as you may think. The result is the last evaluated operand.
In if statement, the evaluated expression is checked whether it is True or False.
Meaning that
if (1 and 3):
is processed in following manner:
1 and 3 = 3
Because Python has to evaluate both operands in this case and result is the last evaluated one, 3.
bool(3) = True
Thus if condition is met.
The conversion to boolean is made internally by Python.
The other languages, for instance Java or C#, require you to write your condition more explicitly.
And if I am not mistaken, C# has two logic operators
docs.microsoft.com/cs-cz/dotnet/cs...
For instance, & evaluates both operands, while && behaves similar to Python's and.
It is important to have good understanding of this, or you may run into hard to find bugs, which are not obvious when you don't know what you are looking at.
By the way, since I wrote this from my mobile, I had no opportunity to check my assertions. Be cautious 😊