I tried to pass the variable args with the args tuple but then the python function complains that the number of arguments is incorrect.
\nPython.Runtime.PythonException: TypeError : invalid number of arguments\n [' File \"C:\\\\Users\\\\henon\\\\AppData\\\\Local\\\\python-3.7.3-embed-amd64\\\\lib\\\\numpy\\\\lib\\\\function_base.py\", line 1013, in gradient\\n raise TypeError(\"invalid number of arguments\")\\n'] at Python.Runtime.PyObject.Invoke(PyTuple args, PyDict kw)\n at Python.Runtime.PyObject.InvokeMethod(String name, PyTuple args, PyDict kw)\nI also tried to pass it as kw[\"varargs\"] but then python says there is no kwarg named \"varargs\"
\nSo how to call that function?
","upvoteCount":1,"answerCount":3,"acceptedAnswer":{"@type":"Answer","text":"You can see the available overloads here:
\n pythonnet/src/runtime/PythonTypes/PyObject.cs\n
\n\n Lines 815 to 930\n in\n 090ff9f\n
\n| \n | /// <summary> | \n
| \n | /// InvokeMethod Method | \n
| \n | /// </summary> | \n
| \n | /// <remarks> | \n
| \n | /// Invoke the named method of the object with the given arguments. | \n
| \n | /// A PythonException is raised if the invocation is unsuccessful. | \n
| \n | /// </remarks> | \n
| \n | public PyObject InvokeMethod(string name, params PyObject[] args) | \n
| \n | { | \n
| \n | if (name == null) throw new ArgumentNullException(nameof(name)); | \n
| \n | if (args == null) throw new ArgumentNullException(nameof(args)); | \n
| \n | if (args.Contains(null)) throw new ArgumentNullException(); | \n
| \n | \n |
| \n | PyObject method = GetAttr(name); | \n
| \n | PyObject result = method.Invoke(args); | \n
| \n | method.Dispose(); | \n
| \n | return result; | \n
| \n | } | \n
| \n | \n |
| \n | \n |
| \n | /// <summary> | \n
| \n | /// InvokeMethod Method | \n
| \n | /// </summary> | \n
| \n | /// <remarks> | \n
| \n | /// Invoke the named method of the object with the given arguments. | \n
| \n | /// A PythonException is raised if the invocation is unsuccessful. | \n
| \n | /// </remarks> | \n
| \n | public PyObject InvokeMethod(string name, PyTuple args) | \n
| \n | { | \n
| \n | if (name == null) throw new ArgumentNullException(nameof(name)); | \n
| \n | if (args == null) throw new ArgumentNullException(nameof(args)); | \n
| \n | \n |
| \n | PyObject method = GetAttr(name); | \n
| \n | PyObject result = method.Invoke(args); | \n
| \n | method.Dispose(); | \n
| \n | return result; | \n
| \n | } | \n
| \n | \n |
| \n | /// <summary> | \n
| \n | /// InvokeMethod Method | \n
| \n | /// </summary> | \n
| \n | /// <remarks> | \n
| \n | /// Invoke the named method of the object with the given arguments. | \n
| \n | /// A PythonException is raised if the invocation is unsuccessful. | \n
| \n | /// </remarks> | \n
| \n | public PyObject InvokeMethod(PyObject name, params PyObject[] args) | \n
| \n | { | \n
| \n | if (name == null) throw new ArgumentNullException(nameof(name)); | \n
| \n | if (args == null) throw new ArgumentNullException(nameof(args)); | \n
| \n | if (args.Contains(null)) throw new ArgumentNullException(); | \n
| \n | \n |
| \n | PyObject method = GetAttr(name); | \n
| \n | PyObject result = method.Invoke(args); | \n
| \n | method.Dispose(); | \n
| \n | return result; | \n
| \n | } | \n
| \n | \n |
| \n | \n |
| \n | /// <summary> | \n
| \n | /// InvokeMethod Method | \n
| \n | /// </summary> | \n
| \n | /// <remarks> | \n
| \n | /// Invoke the named method of the object with the given arguments. | \n
| \n | /// A PythonException is raised if the invocation is unsuccessful. | \n
| \n | /// </remarks> | \n
| \n | public PyObject InvokeMethod(PyObject name, PyTuple args) | \n
| \n | { | \n
| \n | if (name == null) throw new ArgumentNullException(nameof(name)); | \n
| \n | if (args == null) throw new ArgumentNullException(nameof(args)); | \n
| \n | \n |
| \n | PyObject method = GetAttr(name); | \n
| \n | PyObject result = method.Invoke(args); | \n
| \n | method.Dispose(); | \n
| \n | return result; | \n
| \n | } | \n
| \n | \n |
| \n | \n |
| \n | /// <summary> | \n
| \n | /// InvokeMethod Method | \n
| \n | /// </summary> | \n
| \n | /// <remarks> | \n
| \n | /// Invoke the named method of the object with the given arguments | \n
| \n | /// and keyword arguments. Keyword args are passed as a PyDict object. | \n
| \n | /// A PythonException is raised if the invocation is unsuccessful. | \n
| \n | /// </remarks> | \n
| \n | public PyObject InvokeMethod(string name, PyObject[] args, PyDict? kw) | \n
| \n | { | \n
| \n | if (name == null) throw new ArgumentNullException(nameof(name)); | \n
| \n | if (args == null) throw new ArgumentNullException(nameof(args)); | \n
| \n | if (args.Contains(null)) throw new ArgumentNullException(); | \n
| \n | \n |
| \n | PyObject method = GetAttr(name); | \n
| \n | PyObject result = method.Invoke(args, kw); | \n
| \n | method.Dispose(); | \n
| \n | return result; | \n
| \n | } | \n
| \n | \n |
| \n | \n |
| \n | /// <summary> | \n
| \n | /// InvokeMethod Method | \n
| \n | /// </summary> | \n
| \n | /// <remarks> | \n
| \n | /// Invoke the named method of the object with the given arguments | \n
| \n | /// and keyword arguments. Keyword args are passed as a PyDict object. | \n
| \n | /// A PythonException is raised if the invocation is unsuccessful. | \n
| \n | /// </remarks> | \n
| \n | public PyObject InvokeMethod(string name, PyTuple args, PyDict? kw) | \n
| \n | { | \n
| \n | if (name == null) throw new ArgumentNullException(nameof(name)); | \n
| \n | if (args == null) throw new ArgumentNullException(nameof(args)); | \n
| \n | \n |
| \n | PyObject method = GetAttr(name); | \n
| \n | PyObject result = method.Invoke(args, kw); | \n
| \n | method.Dispose(); | \n
| \n | return result; | \n
| \n | } | \n
So we have
\nPyTupleparams PyObject[]PyObject[], PyDictPyTuple, PyDictIt's not ideal as it only has overloads with a PyObject name for the first two functions and the .NET varargs will probably hide the single PyTuple overload (not sure about that).
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 params one), I'd have to check the rules.
Does any of these work?
\nnp.gradient(zX, 4.0, 5.0) (using the fact that np is dynamic, without additional kwArgs)np.InvokeMethod(new PyObject[] {zX, new PyFloat(4.0), new PyFloat(5.0)}, kwArgs)-
|
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.
-
|
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