Is `a
I was curious to see if I could use this a<b<c as a conditional without
using the standard a<b and b<c. So I tried it out and my test results
passed.
a = 1
b = 2
c = 3
assert(a<b<c) # In bounds test
assert(not(b<a<c)) # Out of bounds test
assert(not(a<c<b)) # Out of bounds test
Just for good measure I tried more numbers, this time in the negative
region. Where a, b, c = -10, -9, -8. The test passed once again. Even the
test suit at a higher range works a, b, c = 10, 11, 12. Or even a, b, c =
10, 20, 5.
And the same experiment done in C++. This was my mentality going into it:
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
a=10;
b=20;
c=5;
cout << ((a<b<c)?"True":"False") << endl; // Provides True (wrong)
cout << ((a<b && b<c)?"True":"False") << endl; // Provides False
(proper answer)
return 0;
}
I originally though that this implementation would be invalid since in
every other language I have come across would evaluate a boolean before it
would reach c. With those languages, a<b would evaluate to a boolean and
continuing the evaluation, b<c, would be invalid since it would attempt to
evaluate a boolean against a number (most likely throwing a compile time
error or falsifying the intended comparison). This is a little unsettling
to me for some reason. I guess I just need to be reassured that this is
part of the syntax. It would also be helpful to provide a reference to
where this feature is provided in the Python documentation so I can see to
what extent they provide features like this.
No comments:
Post a Comment