From: Ondrej C. <re...@bu...> - 2008-10-26 22:14:20
|
New submission from Ondrej Certik <on...@ce...>: Run this file: ----------------- class DummyNumber(object): """ Minimal implementation of a number that works with SymPy. If one has a Number class (e.g. Sage Integer, or some other custom class) that one wants to work well with SymPy, one has to implement at least the methods of this class DummyNumber, resp. it's subclasses I5 and F1_1. Basically, one just needs to implement either __int__() or __float__() and then one needs to make sure that the class works with Python integers and with itself. """ def __radd__(self, a): if isinstance(a, (int, float)): return a + self.number return NotImplemented def __add__(self, a): if isinstance(a, (int, float, DummyNumber)): return self.number + a return NotImplemented def __rsub__(self, a): if isinstance(a, (int, float)): return a - self.number return NotImplemented def __sub__(self, a): if isinstance(a, (int, float, DummyNumber)): return self.number - a return NotImplemented def __rmul__(self, a): if isinstance(a, (int, float)): return a * self.number return NotImplemented def __mul__(self, a): if isinstance(a, (int, float, DummyNumber)): return self.number * a return NotImplemented def __rdiv__(self, a): if isinstance(a, (int, float)): return a / self.number return NotImplemented def __div__(self, a): if isinstance(a, (int, float, DummyNumber)): return self.number / a return NotImplemented def __rpow__(self, a): if isinstance(a, (int, float)): return a ** self.number return NotImplemented def __pow__(self, a): if isinstance(a, (int, float, DummyNumber)): return self.number ** a return NotImplemented def __pos__(self): return self.number def __neg__(self): return - self.number class I5(DummyNumber): number = 5 def __int__(self): return self.number class F1_1(DummyNumber): number = 1.1 def __float__(self): return self.number i5 = I5() f1_1 = F1_1() print f1_1**i5 print f1_1**f1_1 ----------------- $ jython w.py Traceback (most recent call last): File "w.py", line 83, in <module> print f1_1**i5 java.lang.NullPointerException at org.python.core.StdoutWrapper.print(StdoutWrapper.java:108) at org.python.core.StdoutWrapper.println(StdoutWrapper.java:167) at org.python.core.Py.println(Py.java:1316) at org.python.pycode._pyx0.f$0(w.py) at org.python.pycode._pyx0.call_function(w.py) at org.python.core.PyTableCode.call(PyTableCode.java:199) at org.python.core.PyCode.call(PyCode.java:14) at org.python.core.Py.runCode(Py.java:1201) at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:155) at org.python.util.jython.run(jython.java:226) at org.python.util.jython.main(jython.java:113) java.lang.NullPointerException: java.lang.NullPointerException ---------- if you comment out the "print f1_1**i5", it fails here: ------------ $ jython w.py Traceback (most recent call last): File "w.py", line 84, in <module> print f1_1**f1_1 TypeError: unsupported operand type(s) for **: 'F1_1' and 'F1_1' ---------- messages: 3703 nosy: certik severity: normal status: open title: custum number and float: unsupported operand type(s) for * _______________________________________ Jython tracker <re...@bu...> <http://bugs.jython.org/issue1159> _______________________________________ |