Skip to content
\n

I tried to pass the variable args with the args tuple but then the python function complains that the number of arguments is incorrect.

\n
Python.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)\n
\n

I also tried to pass it as kw[\"varargs\"] but then python says there is no kwarg named \"varargs\"

\n

So how to call that function?

","upvoteCount":1,"answerCount":3,"acceptedAnswer":{"@type":"Answer","text":"

You can see the available overloads here:

\n
\n

\n pythonnet/src/runtime/PythonTypes/PyObject.cs\n

\n

\n Lines 815 to 930\n in\n 090ff9f\n

\n
\n
\n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n \n \n
/// <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>
public PyObject InvokeMethod(string name, params PyObject[] args)
{
if (name == null) throw new ArgumentNullException(nameof(name));
if (args == null) throw new ArgumentNullException(nameof(args));
if (args.Contains(null)) throw new ArgumentNullException();
PyObject method = GetAttr(name);
PyObject result = method.Invoke(args);
method.Dispose();
return result;
}
/// <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>
public PyObject InvokeMethod(string name, PyTuple args)
{
if (name == null) throw new ArgumentNullException(nameof(name));
if (args == null) throw new ArgumentNullException(nameof(args));
PyObject method = GetAttr(name);
PyObject result = method.Invoke(args);
method.Dispose();
return result;
}
/// <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>
public PyObject InvokeMethod(PyObject name, params PyObject[] args)
{
if (name == null) throw new ArgumentNullException(nameof(name));
if (args == null) throw new ArgumentNullException(nameof(args));
if (args.Contains(null)) throw new ArgumentNullException();
PyObject method = GetAttr(name);
PyObject result = method.Invoke(args);
method.Dispose();
return result;
}
/// <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>
public PyObject InvokeMethod(PyObject name, PyTuple args)
{
if (name == null) throw new ArgumentNullException(nameof(name));
if (args == null) throw new ArgumentNullException(nameof(args));
PyObject method = GetAttr(name);
PyObject result = method.Invoke(args);
method.Dispose();
return result;
}
/// <summary>
/// InvokeMethod Method
/// </summary>
/// <remarks>
/// Invoke the named method of the object with the given arguments
/// and keyword arguments. Keyword args are passed as a PyDict object.
/// A PythonException is raised if the invocation is unsuccessful.
/// </remarks>
public PyObject InvokeMethod(string name, PyObject[] args, PyDict? kw)
{
if (name == null) throw new ArgumentNullException(nameof(name));
if (args == null) throw new ArgumentNullException(nameof(args));
if (args.Contains(null)) throw new ArgumentNullException();
PyObject method = GetAttr(name);
PyObject result = method.Invoke(args, kw);
method.Dispose();
return result;
}
/// <summary>
/// InvokeMethod Method
/// </summary>
/// <remarks>
/// Invoke the named method of the object with the given arguments
/// and keyword arguments. Keyword args are passed as a PyDict object.
/// A PythonException is raised if the invocation is unsuccessful.
/// </remarks>
public PyObject InvokeMethod(string name, PyTuple args, PyDict? kw)
{
if (name == null) throw new ArgumentNullException(nameof(name));
if (args == null) throw new ArgumentNullException(nameof(args));
PyObject method = GetAttr(name);
PyObject result = method.Invoke(args, kw);
method.Dispose();
return result;
}
\n
\n
\n

\n

So we have

\n\n

It'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).

\n

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.

\n

Does any of these work?

\n","upvoteCount":1,"url":"https://github.com/pythonnet/pythonnet/discussions/1772#discussioncomment-2635974"}}}
Discussion options

You must be logged in to vote

You can see the available overloads here:

/// <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>
public PyObject InvokeMethod(string name, params PyObject[] args)
{
if (name == null) throw new ArgumentNullException(nameof(name));
if (args == null) throw new ArgumentNullException(nameof(args));
if (args.Contains(null)) throw new ArgumentNullException();
PyObject method = GetAttr(name);

Replies: 3 comments 5 replies

Comment options

You must be logged in to vote
1 reply
@henon
Comment options

Comment options

You must be logged in to vote
1 reply
@henon
Comment options

Comment options

You must be logged in to vote
3 replies
@henon
Comment options

@henon
Comment options

@henon
Comment options

Answer selected by henon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #1771 on April 25, 2022 08:57.