Comment #6 on issue 2960 by [email protected]: It right 0 < x < 1 ?
http://code.google.com/p/sympy/issues/detail?id=2960
Now that I've had some time actually ponder the problem, I've decided
that "It's not worth it." I believe it to be possible for the general
case, if one were so inclined to introspect the various frames and the
f_lasti attributes, but absolutely not worth it. The root of the problem
is the short-circuit semantics that Python uses in it's statement
generation and evaluation. Specifically, the Python documentation[1] has
this to say on the matter:
"Comparison operations are supported by all objects. They all have the
same priority (which is higher than that of the Boolean operations).
Comparisons can be chained arbitrarily; for example, x < y <= z is
equivalent to x < y and y <= z, except that y is evaluated only once (but
in both cases z is not evaluated at all when x < y is found to be false)."
The parenthesis is a very explicit callout to __nonzero__, but the chained
operation is, unfortunately, evaluated in two separate parts:
x = a < b < c # is converted (effectively) to
x = (a < b) and (b < c).
What we're trying to do is override that, but our efforts are (currently)
resulting in this set of statement transformations:
(1) x = a < b < c
(2) x = (a < b) and (b < c)
(3) x = (LessThanObject) and (b < c)
(4) x = (True) and (b < c)
(5) x = (True) and (LessThanObject)
(6) x = LessThanObject
Though we'd like to return a LessThanObject for the a/b side of (1),
because Python splits the statement conceptually in step 2, the transformed
b object is not transformed for the second comparison. The statement then
morphs into a type of Python ternary statement that looks like:
val = [test] and [new_val] or [other_val]
Doh! Thus, my first five minute hack is, though perhaps "neat," neither
robust nor general, and will not fit the bill.
I think long-term for Sympy, the best suggestion is to put chained
comparisons into the "Gotcha" section, and explain that the suggested
syntax uses parenthesis per comparison:
x = (a < b) < c
or
x = a < (b < c)
[1] http://docs.python.org/library/stdtypes.html
--
You received this message because you are subscribed to the Google Groups
"sympy-issues" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sympy-issues?hl=en.