-
How do you pass a variable list of arguments in pythonnet? The problem is the function Here is an example in python >>> import numpy as np
>>> dx=4.0
>>> dy=5.0
>>> zX=[[1,2,3],[4,5,6],[8,9,0]]
>>> np.gradient(zX, dx, dy)
[array([[ 0.75 , 0.75 , 0.75 ],
[ 0.875, 0.875, -0.375],
[ 1. , 1. , -1.5 ]]), array([[ 0.2, 0.2, 0.2],
[ 0.2, 0.2, 0.2],
[ 0.2, -0.8, -1.8]])]
>>> I tried to pass the variable args with the args tuple but then the python function complains that the number of arguments is incorrect.
I also tried to pass it as kw["varargs"] but then python says there is no kwarg named "varargs" So how to call that function? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
I have an update, looks like I can pass a second tuple after the args tuple and then I am getting a different error so this is a step in the right direction. But I am still stumped as to what the function really expects using (Py.GIL()) {
dynamic np = Py.Import("numpy");
dynamic zX = np.array(new[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 8, 9, 0 } });
Console.WriteLine(zX.__repr__());
var result=(np as PyObject).InvokeMethod("gradient",
new PyTuple(new PyObject[] { zX as PyObject}),
new PyTuple(new PyObject[] { new PyFloat(4.0), new PyFloat(5.0), })
);
Console.WriteLine(result.InvokeMethod("__repr__"));
} Exception:
Any ideas @filmor ? |
Beta Was this translation helpful? Give feedback.
-
This should work according to my understanding: InvokeMethod('gradient', new PyTuple(f, varArg1, varArg2), kwArgs) |
Beta Was this translation helpful? Give feedback.
-
You can see the available overloads here: pythonnet/src/runtime/PythonTypes/PyObject.cs Lines 815 to 930 in 090ff9f So we have
It's not ideal as it only has overloads with a I'm not sure why you'd think any of your attempts here should work. The variant given by @lostmsu could maybe also run into selecting the wrong overload (the Does any of these work?
|
Beta Was this translation helpful? Give feedback.
You can see the available overloads here:
pythonnet/src/runtime/PythonTypes/PyObject.cs
Lines 815 to 930 in 090ff9f