Get names of positional arguments from functions signature

2024/11/18 3:24:44

Using Python 3.x, I'm trying to get the name of all positional arguments from some function i.e:

def foo(a, b, c=1):return

Right now I'm doing this:

from inspect import signature, _empty
args =[x for x, p in signature(foo).parameters.items() if p.default == _empty]

When the function alows *args i.e:

def foo(a, b, c=1, *args):return

I'm adding the line:

args.remove("args")

I was wondering if there is a better way to achieve this.


As suggested by Jim Fasarakis-Hilliard one better way to deal with *args case is using Parameter.kind:

from inspect import signature, Parameter
args =[]
for x, p in signature(foo).parameters.items():if p.default == Parameter.empty and p.kind != Parameter.VAR_POSITIONAL:args.append(x)
Answer

Yes, you can achieve a more robust solution by additionally checking if the .kind attribute of the parameter is not equal to Parameter.VAR_POSITIONAL.

For *args, this is the value that is set when you build the signature object from a function:

>>> def foo(a, b, c=1, *args): pass
>>> print(signature(foo).parameters['args'].kind)
VAR_POSITIONAL

So just import Parameter from inspect and add or the condition that kind != Parameter.VAR_POSITIONAL:

>>> from inspect import Parameter
>>> Parameter.VAR_POSITIONAL == p['args'].kind
True
https://en.xdnf.cn/q/71110.html

Related Q&A

Emacs Python-mode syntax highlighting

I have GNU Emacs 23 (package emacs23) installed on an Ubuntu 10.04 desktop machine and package emacs23-nox installed on an Ubuntu 10.04 headless server (no X installed). Both installations have the sam…

programmatically edit tab order in pyqt4 python

I have a multiple textfield in my form. My problem is the tab order is wrong. Is there a way to edit tab order in code? Just like in QT Designer.thanks.

Getting the parent of a parent widget in Python Tkinter

I am trying to get the parent of a widget then get the parent of that widget. But Everytime I try to I get a error.Error:AttributeError: str object has no attribute _nametowidgetWhy is it giving me tha…

Python Random List Comprehension

I have a list similar to:[1 2 1 4 5 2 3 2 4 5 3 1 4 2] I want to create a list of x random elements from this list where none of the chosen elements are the same. The difficult part is that I would lik…

How to handle unseen categorical values in test data set using python?

Suppose I have location feature. In train data set its unique values are NewYork, Chicago. But in test set it has NewYork, Chicago, London. So while creating one hot encoding how to ignore London? In…

How to get Facebook access token using Python library?

Ive found this Library it seems it is the official one, then found this, but everytime i find an answer the half of it are links to Facebook API Documentation which talks about Javascript or PHP and ho…

How to convert shapefile/geojson to hexagons using uber h3 in python?

I want to create hexagons on my geographic map and want to preserve the digital boundary specified by the shapefile/geojson as well. How do I do it using ubers h3 python library? Im new to shapefiles…

Python mypy: float and int are incompatible types with numbers.Real

I am new to Pythons static typing module mypy. I am trying to append ints and floats to an array, which I typed statically to be Real. But mypy says that they are incompatible types with Real. I though…

Search/Find functionality in QTableView

I have a QWidget and inside that, there is a QTableView. I need to have a find functionality on the first column of the table, so when I click on Ctrl+F, a find dialog will pop-up.class Widget(QWidget)…

How does searching with pip work?

Yes, Im dead serious with this question. How does searching with pip work?The documentation of the keyword search refers to a "pip search reference" at https://pip.pypa.io/en/stable/user_gui…