You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hey, awesome library! I am new to nim and was trying out some basic python integration with nimpy, I am impressed :D
I was wondering if we could try to overload all operators for PyObject so basic stuff like addition and subtraction work out of the box. Currently I managed to do it like this:
import nimpy
let operator =pyImport("operator")
let np =pyImport("numpy")
proc`+`(a: PyObject, b: float): PyObject= operator.add(a, b)
proc`+`(a: PyObject, b: int): PyObject= operator.add(a, b)
echo(np.array([1,2,3]) +20.0)
echo(np.array([1,2,3]) +5)
We would have to do all combinations of operators + types + swapping a and b, maybe this could be automated via e.g. a jinja2 template. They also did this in the PythonKit library for Swift here.
The text was updated successfully, but these errors were encountered:
You can get some of the way there with just making the operator generic in A and B
proc`+`[A, B](a: A, b: B): PyObject=callObject(getAttr(operator, "add"), a, b)
proc`*`[A, B](a: A, b: B): PyObject=callObject(getAttr(operator, "mul"), a, b)
echo(np.array([1,2,3]) +20.0)
echo(20.0+ np.array([1,2,3]))
Hey, awesome library! I am new to nim and was trying out some basic python integration with
nimpy
, I am impressed :DI was wondering if we could try to overload all operators for
PyObject
so basic stuff like addition and subtraction work out of the box. Currently I managed to do it like this:We would have to do all combinations of operators + types + swapping
a
andb
, maybe this could be automated via e.g. ajinja2
template. They also did this in thePythonKit
library for Swift here.The text was updated successfully, but these errors were encountered: