from __future__ import division import ast from sympy import * x, y, z, t = symbols('x y z t') k, m, n = symbols('k m n', integer=True) f, g, h = symbols('f g h', cls=Function) import inspect def op2(a,b): return a*b+a
class AA(object): @staticmethod def __additionFunction__(a1, a2): return a1*a2 #Put what you want instead of this def __multiplyFunction__(a1, a2): return a1*a2+a1 #Put what you want instead of this def __divideFunction__(a1, a2): return a1*a1*a2 #Put what you want instead of this def __init__(self, value): self.value = value def __add__(self, other): return self.value*other.value def __mul__(self, other): return self.value*other.value + other.value def __div__(self, other): return self.value*other.value*other.value solve([AA(x)*AA(y) + AA(-1), AA(x) + AA(-2)], x, y) >>> class AA(object): ... @staticmethod ... def __additionFunction__(a1, a2): ... return a1*a2 #Put what you want instead of this ... def __multiplyFunction__(a1, a2): ... return a1*a2+a1 #Put what you want instead of this ... def __divideFunction__(a1, a2): ... return a1*a1*a2 #Put what you want instead of this ... def __init__(self, value): ... self.value = value ... def __add__(self, other): ... return self.value*other.value ... def __mul__(self, other): ... return self.value*other.value + other.value ... def __div__(self, other): ... return self.value*other.value*other.value ... >>> solve([AA(x)*AA(y) + AA(-1), AA(x) + AA(-2)], x, y) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'Add' and 'AA' On Thursday, December 1, 2016 at 7:19:58 PM UTC+8, Steve D'Aprano wrote: > On Thu, 1 Dec 2016 05:26 pm, Ho Yeung Lee wrote: > > > import ast > > from __future__ import division > > That's not actually your code. That will be a SyntaxError. > > Except in the interactive interpreter, "__future__" imports must be the very > first line of code. > > > > class A: > > @staticmethod > > def __additionFunction__(a1, a2): > > return a1*a2 #Put what you want instead of this > > That cannot work in Python 2, because you are using a "classic" > or "old-style" class. For staticmethod to work correctly, you need to > inherit from object: > > class A(object): > ... > > > Also, do not use double-underscore names for your own functions or methods. > __NAME__ (two leading and two trailing underscores) are reserved for > Python's internal use. You should not invent your own. > > Why do you need this "additionFunction" method for? Why not put this in the > __add__ method? > > > def __add__(self, other): > > return self.__class__.__additionFunction__(self.value, other.value) > > def __mul__(self, other): > > return self.__class__.__multiplyFunction__(self.value, other.value) > > They should be: > > def __add__(self, other): > return self.additionFunction(self.value, other.value) > > def __mul__(self, other): > return self.multiplyFunction(self.value, other.value) > > Or better: > > def __add__(self, other): > return self.value + other.value > > def __mul__(self, other): > return self.value * other.value > > > > -- > Steve > “Cheer up,” they said, “things could be worse.” So I cheered up, and sure > enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list