Python statements returning ints
Is there a way for Python statements to refer to the strings "==" and ">" instead of int bools. I know that I could use the int ( int(1 == 1) ) function or add 0 ( (1 == 1) + 0 ), but I was wondering if there is an easy way to do this. For example, if you want the division to return a float, you can enter from __future__ import division . Is there a way to do this with statements returning ints? Or can I create a class extending the __future__._Feature that will do what I want?
Based on your explanation, you can change the comparison operator to something like:
stack.push(1 if stack.pop() > stack.pop() else 0) This converts the boolean result > to 1 or 0 , as you would like.
Also, be careful when calling stack.pop() twice in the same expression. You donโt know (exactly) in what order the arguments will be evaluated, and various Python implementations may well output the arguments in a different order. You will need temporary variables:
x = stack.pop() y = stack.pop() stack.push(1 if x > y else 0) You cannot override the built-in comparison functions. In a sense, comparison operators already return int . bool is a subclass of int , so you can do something for it that you can do with int. The question then becomes, why do you want comparisons to return int objects, not bool objects?
You can force the comparison operators of your custom classes to return whatever you like - just implement the appropriate methods ( __eq__ , __ne__ , __gt__ , __lt__ , __ge__ , __le__ )). return what you want. For objects that you do not control, you cannot change this, but it should not be necessary: โโbools are ints, due to the principle of replacing Liskov . The code that marks the difference between the bool returned by the __eq__ built-in types and any other integer uses the result incorrectly.
The __future__ module is not __future__ here; you cannot use it to do whatever you want, you can only use it to change certain parameters that have been added in Python. You can turn division into true division with __future__ import, because it was added in Python. The only way to add more __future__ imports is to change Python itself.
On your own objects, it is easy to override each comparison operator. For built-in functions, overriding methods are read-only, so all my attempts to set them are within the scope.
>>> class foo: def __lt__(self, other): return cmp(5, other) >>> f = foo() >>> f<3 1 >>> f<7 -1 >>> f<5 0 >>> j="" >>> j.__lt__=lambda other: cmp(5, other) Traceback (most recent call last): File "<stdin>", line 1, in ? AttributeError: 'str' object attribute '__lt__' is read-only No, you canโt. When Guido unified types and classes, he found a way to override the behavior of built-in types (due to how he implemented things), but he declared this an error and connected a loophole. It is forbidden to change the behavior of built-in types (except for your example, to import a unit from the future, which exists for a good reason).
Sorry, but I canโt find the mailing list. I remember this, although it was quite interesting.