Hi, I am wrapping a class "bigint" from c++ to python. I have overloaded the add method to do something like this on python:
n=bigint(5); n+5L 5
This works well, but if I try to do it vice versa my overloaded add method isn't called by python:
n=bigint(5); 5L+n Causes error.
My add methods look like this: bigint add_bigint_l(bigint const& b, long_ const& l) bigint add_l_bigint(long_ const& l,bigint const& b) and are defined by .def("__add__", add_bigint_l) .def("__add__", add_l_bigint) Can someone tell me howto realize it? Benjamin Schmeling -- GMX im TV ... Die Gedanken sind frei ... Schon gesehen? Jetzt Spot online ansehen: http://www.gmx.net/de/go/tv-spot
Benjamin Schmeling wrote:
Hi,
I am wrapping a class "bigint" from c++ to python. I have overloaded the add method to do something like this on python:
n=bigint(5); n+5L
5
Not 10?
This works well, but if I try to do it vice versa my overloaded add method isn't called by python:
n=bigint(5); 5L+n
Causes error.
My add methods look like this: bigint add_bigint_l(bigint const& b, long_ const& l) bigint add_l_bigint(long_ const& l,bigint const& b)
and are defined by
.def("__add__", add_bigint_l) .def("__add__", add_l_bigint)
Can someone tell me howto realize it?
Remember that Python doesn't support overloading within a class. So it evaluates a + b as a.__add__(b) or if that fails b.__radd__(a), with the "r" indicating that the arguments are reversed. I believe you should change the second line to: .def("__radd__", add_bigint_l) and that there is no need for add_l_bigint. Ben.
participants (2)
-
Ben Hutchings
-
Benjamin Schmeling