Re: Python has Traps; Does Euphoria have Programming Traps?
- Posted by jimcbrown (admin) Aug 20, 2013
- 1881 views
This is simply not true,
Semantics. I think you really agree with _tom here.
The reason you get different results is because the syntax is interpreted differently. The expression 0 <= 5 <= 9 in Python is equivalent to (0 <= 5) and (5 <= 9). Often this notation is used as a short form in Mathematics courses.
Agreed, that this is part of it. It's not the whole story though, as you yourself note below.
It is not that it evaluates 0 <= 5 and quits, instead the Python interpreter sees that it is true and then evaluates 5 <= 9 to ensure the whole expression is true. Ofcourse it is, so it will return true.
Agreed. Euphoria is the same way, e.g. if you had this:
if 0 <= 5 and 5 <= 9 then ret = TRUE else ret = FALSE end if
You'd get the same result.
No short circuit here.
Semantics. See below.
In the other case, 0 <= -5 evaluates to false. So there is no need to evaluate -5 <= 9. This is where short circuit evaluation comes into play.
Agreed. Note again, that we have the same behavior from Euphoria:
if 0 <= -5 and -5 <= 9 then ret = TRUE else ret = FALSE end if
For example: 0 <= 5 <= 3 in Python doesn't evaluate to True, even though 0 <= 5 evaluates to True.
What counts is that Python can and will short-circuit the statement in cases where Euphoria will not do so.
integer x = 0 <= -5 and -5 <= 9
The above Euphoria statement behaves differently than the original Python example, because Euphoria does not perform short-circuiting here. How is this a trap?
Consider:
object s = -1 if sequence(s) and s[1] = -1 then ret = TRUE else ret = FALSE end if
VS:
object s = -1 ret = sequence(s) and s[1] = -1