Title: How to pass *varargs? · pythonnet/pythonnet · Discussion #1772 · GitHub
Open Graph Title: How to pass *varargs? · pythonnet/pythonnet · Discussion #1772
X Title: How to pass *varargs? · pythonnet/pythonnet · Discussion #1772
Description: How to pass *varargs?
Open Graph Description: How do you pass a variable list of arguments in pythonnet? The problem is the function numpy.gradient(f, *varargs, axis=None, edge_order=1) as documented here: https://numpy.org/doc/stable/referenc...
X Description: How do you pass a variable list of arguments in pythonnet? The problem is the function numpy.gradient(f, *varargs, axis=None, edge_order=1) as documented here: https://numpy.org/doc/stable/referenc...
Opengraph URL: https://github.com/pythonnet/pythonnet/discussions/1772
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"QAPage","mainEntity":{"@type":"Question","name":"How to pass *varargs?","text":"How do you pass a variable list of arguments in pythonnet? The problem is the function numpy.gradient(f, *varargs, axis=None, edge_order=1) as documented here: https://numpy.org/doc/stable/reference/generated/numpy.gradient.html
\nHere is an example in python
\n>>> import numpy as np\n>>> dx=4.0\n>>> dy=5.0\n>>> zX=[[1,2,3],[4,5,6],[8,9,0]]\n>>> np.gradient(zX, dx, dy)\n[array([[ 0.75 , 0.75 , 0.75 ],\n [ 0.875, 0.875, -0.375],\n [ 1. , 1. , -1.5 ]]), array([[ 0.2, 0.2, 0.2],\n [ 0.2, 0.2, 0.2],\n [ 0.2, -0.8, -1.8]])]\n>>>
\nI 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)\n
\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 \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 /// <summary> \n \n\n \n \n /// InvokeMethod Method \n \n\n \n \n /// </summary> \n \n\n \n \n /// <remarks> \n \n\n \n \n /// Invoke the named method of the object with the given arguments. \n \n\n \n \n /// A PythonException is raised if the invocation is unsuccessful. \n \n\n \n \n /// </remarks> \n \n\n \n \n public PyObject InvokeMethod(string name, params PyObject[] args) \n \n\n \n \n { \n \n\n \n \n if (name == null) throw new ArgumentNullException(nameof(name)); \n \n\n \n \n if (args == null) throw new ArgumentNullException(nameof(args)); \n \n\n \n \n if (args.Contains(null)) throw new ArgumentNullException(); \n \n\n \n \n \n \n\n \n \n PyObject method = GetAttr(name); \n \n\n \n \n PyObject result = method.Invoke(args); \n \n\n \n \n method.Dispose(); \n \n\n \n \n return result; \n \n\n \n \n } \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n /// <summary> \n \n\n \n \n /// InvokeMethod Method \n \n\n \n \n /// </summary> \n \n\n \n \n /// <remarks> \n \n\n \n \n /// Invoke the named method of the object with the given arguments. \n \n\n \n \n /// A PythonException is raised if the invocation is unsuccessful. \n \n\n \n \n /// </remarks> \n \n\n \n \n public PyObject InvokeMethod(string name, PyTuple args) \n \n\n \n \n { \n \n\n \n \n if (name == null) throw new ArgumentNullException(nameof(name)); \n \n\n \n \n if (args == null) throw new ArgumentNullException(nameof(args)); \n \n\n \n \n \n \n\n \n \n PyObject method = GetAttr(name); \n \n\n \n \n PyObject result = method.Invoke(args); \n \n\n \n \n method.Dispose(); \n \n\n \n \n return result; \n \n\n \n \n } \n \n\n \n \n \n \n\n \n \n /// <summary> \n \n\n \n \n /// InvokeMethod Method \n \n\n \n \n /// </summary> \n \n\n \n \n /// <remarks> \n \n\n \n \n /// Invoke the named method of the object with the given arguments. \n \n\n \n \n /// A PythonException is raised if the invocation is unsuccessful. \n \n\n \n \n /// </remarks> \n \n\n \n \n public PyObject InvokeMethod(PyObject name, params PyObject[] args) \n \n\n \n \n { \n \n\n \n \n if (name == null) throw new ArgumentNullException(nameof(name)); \n \n\n \n \n if (args == null) throw new ArgumentNullException(nameof(args)); \n \n\n \n \n if (args.Contains(null)) throw new ArgumentNullException(); \n \n\n \n \n \n \n\n \n \n PyObject method = GetAttr(name); \n \n\n \n \n PyObject result = method.Invoke(args); \n \n\n \n \n method.Dispose(); \n \n\n \n \n return result; \n \n\n \n \n } \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n /// <summary> \n \n\n \n \n /// InvokeMethod Method \n \n\n \n \n /// </summary> \n \n\n \n \n /// <remarks> \n \n\n \n \n /// Invoke the named method of the object with the given arguments. \n \n\n \n \n /// A PythonException is raised if the invocation is unsuccessful. \n \n\n \n \n /// </remarks> \n \n\n \n \n public PyObject InvokeMethod(PyObject name, PyTuple args) \n \n\n \n \n { \n \n\n \n \n if (name == null) throw new ArgumentNullException(nameof(name)); \n \n\n \n \n if (args == null) throw new ArgumentNullException(nameof(args)); \n \n\n \n \n \n \n\n \n \n PyObject method = GetAttr(name); \n \n\n \n \n PyObject result = method.Invoke(args); \n \n\n \n \n method.Dispose(); \n \n\n \n \n return result; \n \n\n \n \n } \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n /// <summary> \n \n\n \n \n /// InvokeMethod Method \n \n\n \n \n /// </summary> \n \n\n \n \n /// <remarks> \n \n\n \n \n /// Invoke the named method of the object with the given arguments \n \n\n \n \n /// and keyword arguments. Keyword args are passed as a PyDict object. \n \n\n \n \n /// A PythonException is raised if the invocation is unsuccessful. \n \n\n \n \n /// </remarks> \n \n\n \n \n public PyObject InvokeMethod(string name, PyObject[] args, PyDict? kw) \n \n\n \n \n { \n \n\n \n \n if (name == null) throw new ArgumentNullException(nameof(name)); \n \n\n \n \n if (args == null) throw new ArgumentNullException(nameof(args)); \n \n\n \n \n if (args.Contains(null)) throw new ArgumentNullException(); \n \n\n \n \n \n \n\n \n \n PyObject method = GetAttr(name); \n \n\n \n \n PyObject result = method.Invoke(args, kw); \n \n\n \n \n method.Dispose(); \n \n\n \n \n return result; \n \n\n \n \n } \n \n\n \n \n \n \n\n \n \n \n \n\n \n \n /// <summary> \n \n\n \n \n /// InvokeMethod Method \n \n\n \n \n /// </summary> \n \n\n \n \n /// <remarks> \n \n\n \n \n /// Invoke the named method of the object with the given arguments \n \n\n \n \n /// and keyword arguments. Keyword args are passed as a PyDict object. \n \n\n \n \n /// A PythonException is raised if the invocation is unsuccessful. \n \n\n \n \n /// </remarks> \n \n\n \n \n public PyObject InvokeMethod(string name, PyTuple args, PyDict? kw) \n \n\n \n \n { \n \n\n \n \n if (name == null) throw new ArgumentNullException(nameof(name)); \n \n\n \n \n if (args == null) throw new ArgumentNullException(nameof(args)); \n \n\n \n \n \n \n\n \n \n PyObject method = GetAttr(name); \n \n\n \n \n PyObject result = method.Invoke(args, kw); \n \n\n \n \n method.Dispose(); \n \n\n \n \n return result; \n \n\n \n \n } \n \n
\n \n\n\nSo we have
\n\nPyTuple \nparams PyObject[] \nPyObject[], PyDict \nPyTuple, PyDict \n
\nIt'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).
\nI'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.
\nDoes any of these work?
\n\nnp.gradient(zX, 4.0, 5.0) (using the fact that np is dynamic, without additional kwArgs) \nnp.InvokeMethod(new PyObject[] {zX, new PyFloat(4.0), new PyFloat(5.0)}, kwArgs) \n
","upvoteCount":1,"url":"https://github.com/pythonnet/pythonnet/discussions/1772#discussioncomment-2635974"}}}
| route-pattern | /_view_fragments/Voltron::DiscussionsFragmentsController/show/:user_id/:repository/:discussion_number/discussion_layout(.:format) |
| route-controller | voltron_discussions_fragments |
| route-action | discussion_layout |
| fetch-nonce | v2:37bd6afc-77ac-d04c-a8e8-a99dd955a75d |
| current-catalog-service-hash | 9f0abe34da433c9b6db74bffa2466494a717b579a96b30a5d252e5090baea7be |
| request-id | A8FA:3A10FF:2F23965:3EFDC54:6971D9C9 |
| html-safe-nonce | 4f3d81b32ee6458cc8d51596598e83154e5bd8868f8c372eb5bee21f5e921e5e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOEZBOjNBMTBGRjoyRjIzOTY1OjNFRkRDNTQ6Njk3MUQ5QzkiLCJ2aXNpdG9yX2lkIjoiMjcyMDgxMDU5MjA2OTg2Nzk3NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 24fbf0e06f055ead6be1ef3cb57e1530b8ac35102115304390c2398097498ac3 |
| hovercard-subject-tag | discussion:4031933 |
| github-keyboard-shortcuts | repository,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/Voltron::DiscussionsFragmentsController/show/pythonnet/pythonnet/1772/discussion_layout |
| twitter:image | https://opengraph.githubassets.com/be387ce3c5ae98f1034eefabd85417cffd0148e82f65346e422bd45f53d8319d/pythonnet/pythonnet/discussions/1772 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/be387ce3c5ae98f1034eefabd85417cffd0148e82f65346e422bd45f53d8319d/pythonnet/pythonnet/discussions/1772 |
| og:image:alt | How do you pass a variable list of arguments in pythonnet? The problem is the function numpy.gradient(f, *varargs, axis=None, edge_order=1) as documented here: https://numpy.org/doc/stable/referenc... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7476eb4140129667a7530d10cfb7688f701883e35a4dcaa4673e3ec599af5199 |
| turbo-cache-control | no-preview |
| go-import | github.com/pythonnet/pythonnet git https://github.com/pythonnet/pythonnet.git |
| octolytics-dimension-user_id | 6050430 |
| octolytics-dimension-user_login | pythonnet |
| octolytics-dimension-repository_id | 14748123 |
| octolytics-dimension-repository_nwo | pythonnet/pythonnet |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 14748123 |
| octolytics-dimension-repository_network_root_nwo | pythonnet/pythonnet |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 2cc0827c872b538cd08371730242ae4951d2d61a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width