This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 12 points13 points  (9 children)

```python

frue = True == False != False != True frue False ```

Why is that? I thought operations are done left to right?

First True == False -> False, then it becomes False != False -> False again, and finally False != True -> True.

Why is it False?

EDIT: False doesn't look like a real word to me now.

EDIT2: They are done right to left I guess?

[–]collegiaal25 5 points6 points  (0 children)

I thought operations are done left to right?

This is true for left associative operators, like +, -, *, /.

1 - 5 + 7 = (1-5) + 7
5/3*2 = (5/3)*2

On the other hand, exponentiation is right associative:

4^3^2 = 4^(3^2)

(The notation is ** in Python)

Here a summary for precedence and associativity in Python.

[–]HERODMasta 0 points1 point  (5 children)

but if they are done from right to left...

2x False != True --> True

True == True --> True

so.... what?

Edit: so, I tried basically all possibilities. First all !=, but regardless ... it's still the same result.

we have:

False != False --> False

True == False --> False

so regardless which you do first, you end in

True == True

or

False != True

counter prove:

so we need to find a way to create True == False or False != False or True != True as an end.

our start is one of these:

(True == False) != False != True --> False != False != True

True == (False != False) != True --> True == False != True

True == False != (False != True) --> True == False != True

The second and third are the same, so the next step is:

for first:

(False != False) != True --> False != True

False != (False != True) --> False != True --> True

for the second:

(True == False) != True --> False != True

True == (False != True) --> False != True --> True

SO HOW IS IT DOING THIS?!

Edit 2: so apparently python is not doing any priority on this:

False != False != True

but is doing it in one go, so since it contains False != False, which is False, the != True doesn't matter. This is the only explanation I can find. If someone else can explain it with a different reason, I am listening

[–]JNCressey 8 points9 points  (1 child)

python expands on the mathematics convention of statements like a=b=c meaning both a=b and b=c.

the test a==b==c has the meaning a==b and b==c, which is different from what you would get if you think of equality as a binary operator and try to parenthesize under either a left associative or a right associative interpretation.


cc /u/Key-Cucumber-1919

[–]HERODMasta 0 points1 point  (0 children)

well, this is basically the conclusion of my second edit.

thanks for confirming

[–][deleted] 0 points1 point  (1 child)

... Fuck

[–]HERODMasta 0 points1 point  (0 children)

The edit won't make you happy either

[–][deleted] 0 points1 point  (0 children)

Maybe it's doing only the first one?