- Notifications
You must be signed in to change notification settings - Fork750
-
How do you pass a variable list of arguments in pythonnet? The problem is the function Here is an example in python >>>importnumpyasnp>>>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? |
BetaWas this translation helpful?Give feedback.
All reactions
You can see the available overloads here:
pythonnet/src/runtime/PythonTypes/PyObject.cs
Lines 815 to 930 in090ff9f
/// <summary> | |
/// InvokeMethod Method | |
/// </summary> | |
/// <remarks> | |
/// Invoke the named method of the object with the given arguments. | |
/// A PythonException is raised if the invocation is unsuccessful. | |
/// </remarks> | |
publicPyObjectInvokeMethod(stringname,paramsPyObject[]args) | |
{ | |
if(name==null)thrownewArgumentNullException(nameof(name)); | |
if(args==null)thrownewArgumentNullException(nameof(args)); | |
if(args.Contains(null))thrownewArgumentNullException(); | |
PyObjectmethod=GetAttr(name); |
Replies: 3 comments 5 replies
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
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()){dynamicnp=Py.Import("numpy");dynamiczX=np.array(new[,]{{1,2,3},{4,5,6},{8,9,0}});Console.WriteLine(zX.__repr__());varresult=(npasPyObject).InvokeMethod("gradient",newPyTuple(newPyObject[]{zXasPyObject}),newPyTuple(newPyObject[]{newPyFloat(4.0),newPyFloat(5.0),}));Console.WriteLine(result.InvokeMethod("__repr__"));} Exception:
Any ideas@filmor ? |
BetaWas this translation helpful?Give feedback.
All reactions
-
Nah, looks like the second tuple is interpreted as kwargs because I can't pass additional kw args: varresult=(npasPyObject).InvokeMethod("gradient",newPyTuple(newPyObject[]{zXasPyObject}),newPyTuple(newPyObject[]{newPyFloat(4.0),newPyFloat(5.0),}),newPyDict(){["edge_order"]=newPyInt(1)}); This again leads to invalid arguments error |
BetaWas this translation helpful?Give feedback.
All reactions
-
This should work according to my understanding: InvokeMethod('gradient',newPyTuple(f,varArg1,varArg2),kwArgs) |
BetaWas this translation helpful?Give feedback.
All reactions
-
Yeah, that was what I would have expected too but it does not. That was what I tried initially. It gives |
BetaWas this translation helpful?Give feedback.
All reactions
-
You can see the available overloads here: pythonnet/src/runtime/PythonTypes/PyObject.cs Lines 815 to 930 in090ff9f
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?
|
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Thanks for your time looking into this. Both your suggestions don't work. The dynamic version gives |
BetaWas this translation helpful?Give feedback.
All reactions
-
I reopened this as an issue, as it is impossible to pass the *varargs.#1804 |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
OK, I was wrong,@filmor 's answer works with the current preview version of pythonnet, it just had a typo. The working code is: varkwArgs=newPyDict(){["edge_order"]=newPyInt(1)};varresult=(npasPyObject).InvokeMethod("gradient",newPyObject[]{(zXasPyObject),newPyFloat(4.0),newPyFloat(5.0)},kwArgs); |
BetaWas this translation helpful?Give feedback.
All reactions
This discussion was converted from issue #1771 on April 25, 2022 08:57.