Re: compile error when using override
from __future__ import division 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) 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) >>> solve([AA(x)*AA(y) + AA(-1), AA(x) + AA(-2)], x, y) Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for +: 'Add' and 'AA' still error, actually i invented 3 valued logic algebraic operation which is quintessential and would replace into it if this succeed On Friday, December 2, 2016 at 1:02:19 PM UTC+8, Steve D'Aprano wrote: > On Fri, 2 Dec 2016 01:35 pm, Ho Yeung Lee wrote: > > > 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 > > Neither ast nor inspect is used. Why import them? > > The only symbols you are using are x and y. > > > > def op2(a,b): > > return a*b+a > > This doesn't seem to be used. Get rid of it. > > > > 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 > > None of those methods are used. Get rid of them. > > > def __init__(self, value): > > self.value = value > > def __add__(self, other): > > return self.value*other.value > > Sorry, you want AA(5) + AA(2) to return 10? > > > 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) > > I don't understand what you are trying to do here. What result are you > execting? > > Maybe you just want this? > > from sympy import solve, symbols > x, y = symbols('x y') > print( solve([x*y - 1, x - 2], x, y) ) > > which prints the result: > [(2, 1/2)] > > > Perhaps if you explain what you are trying to do, we can help better. > > But please, cut down your code to only code that is being used! > > > > > -- > 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
Re: compile error when using override
On Fri, 2 Dec 2016 01:35 pm, Ho Yeung Lee wrote: > 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 Neither ast nor inspect is used. Why import them? The only symbols you are using are x and y. > def op2(a,b): > return a*b+a This doesn't seem to be used. Get rid of it. > 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 None of those methods are used. Get rid of them. > def __init__(self, value): > self.value = value > def __add__(self, other): > return self.value*other.value Sorry, you want AA(5) + AA(2) to return 10? > 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) I don't understand what you are trying to do here. What result are you execting? Maybe you just want this? from sympy import solve, symbols x, y = symbols('x y') print( solve([x*y - 1, x - 2], x, y) ) which prints the result: [(2, 1/2)] Perhaps if you explain what you are trying to do, we can help better. But please, cut down your code to only code that is being used! -- 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
Re: compile error when using override
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 "", line 1, in 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
Re: compile error when using override
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