如何获取方法参数名称?

发布于 2024-07-06 19:29:08 字数 192 浏览 14 评论 0原文

鉴于函数 a_method 的定义类似于

def a_method(arg1, arg2):
    pass

a_method 本身开始,我如何获取参数名称 - 例如,作为字符串元组,如 ( “arg1”,“arg2”)?

Given that a function a_method has been defined like

def a_method(arg1, arg2):
    pass

Starting from a_method itself, how can I get the argument names - for example, as a tuple of strings, like ("arg1", "arg2")?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(19

浪漫人生路 2024-07-13 19:29:09

返回参数名称列表,处理部分函数和常规函数:

def get_func_args(f):
    if hasattr(f, 'args'):
        return f.args
    else:
        return list(inspect.signature(f).parameters)

Returns a list of argument names, takes care of partials and regular functions:

def get_func_args(f):
    if hasattr(f, 'args'):
        return f.args
    else:
        return list(inspect.signature(f).parameters)
一梦等七年七年为一梦 2024-07-13 19:29:09

更新 Brian 的回答

如果 Python 3 中的函数有仅关键字参数,那么您需要使用inspect.getfullargspec:

def yay(a, b=10, *, c=20, d=30):
    pass
inspect.getfullargspec(yay)

产生以下结果:

FullArgSpec(args=['a', 'b'], varargs=None, varkw=None, defaults=(10,), kwonlyargs=['c', 'd'], kwonlydefaults={'c': 20, 'd': 30}, annotations={})

Update for Brian's answer:

If a function in Python 3 has keyword-only arguments, then you need to use inspect.getfullargspec:

def yay(a, b=10, *, c=20, d=30):
    pass
inspect.getfullargspec(yay)

yields this:

FullArgSpec(args=['a', 'b'], varargs=None, varkw=None, defaults=(10,), kwonlyargs=['c', 'd'], kwonlydefaults={'c': 20, 'd': 30}, annotations={})
土豪我们做朋友吧 2024-07-13 19:29:09

在 python 3 中,下面是将 *args**kwargs 制作成 dict (对于 python 使用 OrderedDict <3.6 维护dict命令):

from functools import wraps

def display_param(func):
    @wraps(func)
    def wrapper(*args, **kwargs):

        param = inspect.signature(func).parameters
        all_param = {
            k: args[n] if n < len(args) else v.default
            for n, (k, v) in enumerate(param.items()) if k != 'kwargs'
        }
        all_param .update(kwargs)
        print(all_param)

        return func(**all_param)
    return wrapper

In python 3, below is to make *args and **kwargs into a dict (use OrderedDict for python < 3.6 to maintain dict orders):

from functools import wraps

def display_param(func):
    @wraps(func)
    def wrapper(*args, **kwargs):

        param = inspect.signature(func).parameters
        all_param = {
            k: args[n] if n < len(args) else v.default
            for n, (k, v) in enumerate(param.items()) if k != 'kwargs'
        }
        all_param .update(kwargs)
        print(all_param)

        return func(**all_param)
    return wrapper
Saygoodbye 2024-07-13 19:29:09

操作某些函数的参数名称的最简单方法:

parameters_list = list(inspect.signature(self.YOUR_FUNCTION).parameters))

结果:

['YOUR_FUNCTION_parameter_name_0', 'YOUR_FUNCTION_parameter_name_1', ...]

由于您获得了特定的参数名称,因此这种方法会更容易:

parameters_list = list(inspect.signature(self.YOUR_FUNCTION).parameters)[0]

结果:

'YOUR_FUNCTION_parameter_name_0'

Easiest way to manipulate parameters names of some function:

parameters_list = list(inspect.signature(self.YOUR_FUNCTION).parameters))

Result:

['YOUR_FUNCTION_parameter_name_0', 'YOUR_FUNCTION_parameter_name_1', ...]

Making this way will be even easier since you get the specific one:

parameters_list = list(inspect.signature(self.YOUR_FUNCTION).parameters)[0]

Result:

'YOUR_FUNCTION_parameter_name_0'
惜醉颜 2024-07-13 19:29:09

对于像我一样正在寻找一种解决方案的人,我还有另一个建议,该解决方案将装饰器中的所有参数及其值(默认或非默认)放入字典中。

import inspect

def get_arguments(func, args, kwargs, is_method=False):
    offset = 1 if is_method else 0
    specs = inspect.getfullargspec(func)
    d = {}
    for i, parameter in enumerate(specs.args[offset:]):
        i += offset
        if i < len(args):
            d[parameter] = args[i]
        elif parameter in kwargs:
            d[parameter] = kwargs[parameter]
        else:
            d[parameter] = specs.defaults[i - len(args)]
    return d

现在在像这样的装饰器中打印 get_arguments 的返回值

def a_function_decorator(func):
    def inner(*args, **kwargs):
        print(get_arguments(func, args, kwargs))
        return func(*args, **kwargs)

    return inner

并将其应用到类似的函数上

@a_function_decorator
def foo(a, b, c="default_c", d="default_d"):
    pass

将为我们提供

foo(1, 2, d="eek")
# {'a': 1, 'b': 2, 'c': 'default_c', 'd': 'eek'}

foo(1, 2, "blah")
# {'a': 1, 'b': 2, 'c': 'blah', 'd': 'default_c'}

foo(1, 2)
# {'a': 1, 'b': 2, 'c': 'default_c', 'd': 'default_d'}

相同的方法

def a_method_decorator(func):
    def inner(*args, **kwargs):
        print(get_arguments(func, args, kwargs, is_method=True))
        return func(*args, **kwargs)

    return inner

class Bar:
    @a_method_decorator
    def foo(self, a, b, c="default_c", d="default_d"):
        pass

Bar().foo(1, 2, d="eek")
#{'a': 1, 'b': 2, 'c': 'default_c', 'd': 'eek'}
Bar().foo(1, 2, "blah")
# {'a': 1, 'b': 2, 'c': 'blah', 'd': 'default_c'}
Bar().foo(1, 2)
# {'a': 1, 'b': 2, 'c': 'default_c', 'd': 'default_d'}

这当然不是最漂亮的解决方案,但这是我见过的第一个解决方案这正是我想要的。

I have another suggestion for those who, like me, are looking for a solution that puts inside a decorator all parameters and their values (default or not) into a dictonary.

import inspect

def get_arguments(func, args, kwargs, is_method=False):
    offset = 1 if is_method else 0
    specs = inspect.getfullargspec(func)
    d = {}
    for i, parameter in enumerate(specs.args[offset:]):
        i += offset
        if i < len(args):
            d[parameter] = args[i]
        elif parameter in kwargs:
            d[parameter] = kwargs[parameter]
        else:
            d[parameter] = specs.defaults[i - len(args)]
    return d

Now printing the return value of get_arguments inside a decorator like this one

def a_function_decorator(func):
    def inner(*args, **kwargs):
        print(get_arguments(func, args, kwargs))
        return func(*args, **kwargs)

    return inner

and apply it on a function like

@a_function_decorator
def foo(a, b, c="default_c", d="default_d"):
    pass

will give us

foo(1, 2, d="eek")
# {'a': 1, 'b': 2, 'c': 'default_c', 'd': 'eek'}

foo(1, 2, "blah")
# {'a': 1, 'b': 2, 'c': 'blah', 'd': 'default_c'}

foo(1, 2)
# {'a': 1, 'b': 2, 'c': 'default_c', 'd': 'default_d'}

Same works for methods

def a_method_decorator(func):
    def inner(*args, **kwargs):
        print(get_arguments(func, args, kwargs, is_method=True))
        return func(*args, **kwargs)

    return inner

class Bar:
    @a_method_decorator
    def foo(self, a, b, c="default_c", d="default_d"):
        pass

Bar().foo(1, 2, d="eek")
#{'a': 1, 'b': 2, 'c': 'default_c', 'd': 'eek'}
Bar().foo(1, 2, "blah")
# {'a': 1, 'b': 2, 'c': 'blah', 'd': 'default_c'}
Bar().foo(1, 2)
# {'a': 1, 'b': 2, 'c': 'default_c', 'd': 'default_d'}

It's certainly not the prettiest solution, but it's the first one I've seen that does exactly what I want.

给我一枪 2024-07-13 19:29:09

从 python 3.0 开始,简单易读的答案:

import inspect


args_names = inspect.signature(function).parameters.keys()
args_dict = {
    **dict(zip(args_names, args)),
    **kwargs,
}

Simple easy to read answer as of python 3.0 onwards:

import inspect


args_names = inspect.signature(function).parameters.keys()
args_dict = {
    **dict(zip(args_names, args)),
    **kwargs,
}
迟月 2024-07-13 19:29:09

要更新一点 Brian 的答案,现在有一个很好的 inspect.signature 向后移植,您可以在较旧的 python 版本中使用:funcsigs
所以我个人的偏好是为了

try:  # python 3.3+
    from inspect import signature
except ImportError:
    from funcsigs import signature

def aMethod(arg1, arg2):
    pass

sig = signature(aMethod)
print(sig)

好玩,如果你有兴趣使用 Signature 对象,甚至动态创建具有随机签名的函数,你可以看看我的 makefun 项目。

To update a little bit Brian's answer, there is now a nice backport of inspect.signature that you can use in older python versions: funcsigs.
So my personal preference would go for

try:  # python 3.3+
    from inspect import signature
except ImportError:
    from funcsigs import signature

def aMethod(arg1, arg2):
    pass

sig = signature(aMethod)
print(sig)

For fun, if you're interested in playing with Signature objects and even creating functions with random signatures dynamically you can have a look at my makefun project.

野味少女 2024-07-13 19:29:09

我在谷歌上搜索如何打印函数名称并为作业提供参数,我必须创建一个装饰器来打印它们,我使用了这个:

def print_func_name_and_args(func):
    
    def wrapper(*args, **kwargs):
    print(f"Function name: '{func.__name__}' supplied args: '{args}'")
    func(args[0], args[1], args[2])
    return wrapper


@print_func_name_and_args
def my_function(n1, n2, n3):
    print(n1 * n2 * n3)
    
my_function(1, 2, 3)

#Function name: 'my_function' supplied args: '(1, 2, 3)'

I was googling to find how to print function name and supplied arguments for an assignment I had to create a decorator to print them and I used this:

def print_func_name_and_args(func):
    
    def wrapper(*args, **kwargs):
    print(f"Function name: '{func.__name__}' supplied args: '{args}'")
    func(args[0], args[1], args[2])
    return wrapper


@print_func_name_and_args
def my_function(n1, n2, n3):
    print(n1 * n2 * n3)
    
my_function(1, 2, 3)

#Function name: 'my_function' supplied args: '(1, 2, 3)'
萌面超妹 2024-07-13 19:29:09

现在 dir()vars() 怎么样?

似乎完全按照要求做的超级简单......

必须从函数范围内调用。

但要小心它会返回所有局部变量,所以一定要在如果需要的话,可以在函数的最开始处。

另请注意,正如评论中所指出的,这不允许从范围之外完成。 所以不完全是OP的场景,但仍然符合问题标题。 因此我的回答是这样的。

What about dir() and vars() now?

Seems doing exactly what is being asked super simply…

Must be called from within the function scope.

But be wary that it will return all local variables so be sure to do it at the very beginning of the function if needed.

Also note that, as pointed out in the comments, this doesn't allow it to be done from outside the scope. So not exactly OP's scenario but still matches the question title. Hence my answer.

三岁铭 2024-07-13 19:29:08

看一下 inspect 模块 - 这将执行以下操作为您检查各种代码对象属性。

>>> inspect.getfullargspec(a_method)
(['arg1', 'arg2'], None, None, None)

其他结果是 *args 和 **kwargs 变量的名称以及提供的默认值。 IE。

>>> def foo(a, b, c=4, *arglist, **keywords): pass
>>> inspect.getfullargspec(foo)
(['a', 'b', 'c'], 'arglist', 'keywords', (4,))

请注意,某些可调用对象在 Python 的某些实现中可能无法自省。 例如,在 CPython 中,C 中定义的一些内置函数不提供有关其参数的元数据。 因此,如果您在内置函数上使用 inspect.getfullargspec(),您将收到 ValueError

从 Python 3.3 开始,您可以使用 inspect .signature() 查看可调用对象的调用签名:

>>> inspect.signature(foo)
<Signature (a, b, c=4, *arglist, **keywords)>

Take a look at the inspect module - this will do the inspection of the various code object properties for you.

>>> inspect.getfullargspec(a_method)
(['arg1', 'arg2'], None, None, None)

The other results are the name of the *args and **kwargs variables, and the defaults provided. ie.

>>> def foo(a, b, c=4, *arglist, **keywords): pass
>>> inspect.getfullargspec(foo)
(['a', 'b', 'c'], 'arglist', 'keywords', (4,))

Note that some callables may not be introspectable in certain implementations of Python. For Example, in CPython, some built-in functions defined in C provide no metadata about their arguments. As a result, you will get a ValueError if you use inspect.getfullargspec() on a built-in function.

Since Python 3.3, you can use inspect.signature() to see the call signature of a callable object:

>>> inspect.signature(foo)
<Signature (a, b, c=4, *arglist, **keywords)>
帅气称霸 2024-07-13 19:29:08

在 CPython 中,参数的数量为

a_method.func_code.co_argcount

且它们的名称位于这些是 CPython 的实现细节的开头

a_method.func_code.co_varnames

,因此这可能不适用于其他 Python 实现,例如 IronPython 和 Jython。

允许“传递”参数的一种可移植方法是使用签名 func(*args, **kwargs) 定义函数。 这在例如 matplotlib 中被大量使用,其中外部 API 层将大量关键字参数传递给较低级别​​的 API。

In CPython, the number of arguments is

a_method.func_code.co_argcount

and their names are in the beginning of

a_method.func_code.co_varnames

These are implementation details of CPython, so this probably does not work in other implementations of Python, such as IronPython and Jython.

One portable way to admit "pass-through" arguments is to define your function with the signature func(*args, **kwargs). This is used a lot in e.g. matplotlib, where the outer API layer passes lots of keyword arguments to the lower-level API.

因为看清所以看轻 2024-07-13 19:29:08

Python 3 版本是:

def _get_args_dict(fn, args, kwargs):
    args_names = fn.__code__.co_varnames[:fn.__code__.co_argcount]
    return {**dict(zip(args_names, args)), **kwargs}

该方法返回一个包含 args 和 kwargs 的字典。

The Python 3 version is:

def _get_args_dict(fn, args, kwargs):
    args_names = fn.__code__.co_varnames[:fn.__code__.co_argcount]
    return {**dict(zip(args_names, args)), **kwargs}

The method returns a dictionary containing both args and kwargs.

难以启齿的温柔 2024-07-13 19:29:08

在装饰器方法中,您可以通过以下方式列出原始方法的参数:

import inspect, itertools 

def my_decorator():   
        def decorator(f):
            def wrapper(*args, **kwargs):
                # if you want arguments names as a list:
                args_name = inspect.getargspec(f)[0]
                print(args_name)

                # if you want names and values as a dictionary:
                args_dict = dict(itertools.izip(args_name, args))
                print(args_dict)

                # if you want values as a list:
                args_values = args_dict.values()
                print(args_values)

如果 **kwargs 对您很重要,那么它会有点复杂:

def wrapper(*args, **kwargs):
    args_name = list(OrderedDict.fromkeys(inspect.getargspec(f)[0] + kwargs.keys()))
    args_dict = OrderedDict(list(itertools.izip(args_name, args)) + list(kwargs.iteritems()))
    args_values = args_dict.values()

示例:

@my_decorator()
def my_function(x, y, z=3):
    pass


my_function(1, y=2, z=3, w=0)
# prints:
# ['x', 'y', 'z', 'w']
# {'y': 2, 'x': 1, 'z': 3, 'w': 0}
# [1, 2, 3, 0]

In a decorator method, you can list arguments of the original method in this way:

import inspect, itertools 

def my_decorator():   
        def decorator(f):
            def wrapper(*args, **kwargs):
                # if you want arguments names as a list:
                args_name = inspect.getargspec(f)[0]
                print(args_name)

                # if you want names and values as a dictionary:
                args_dict = dict(itertools.izip(args_name, args))
                print(args_dict)

                # if you want values as a list:
                args_values = args_dict.values()
                print(args_values)

If the **kwargs are important for you, then it will be a bit complicated:

def wrapper(*args, **kwargs):
    args_name = list(OrderedDict.fromkeys(inspect.getargspec(f)[0] + kwargs.keys()))
    args_dict = OrderedDict(list(itertools.izip(args_name, args)) + list(kwargs.iteritems()))
    args_values = args_dict.values()

Example:

@my_decorator()
def my_function(x, y, z=3):
    pass


my_function(1, y=2, z=3, w=0)
# prints:
# ['x', 'y', 'z', 'w']
# {'y': 2, 'x': 1, 'z': 3, 'w': 0}
# [1, 2, 3, 0]
染柒℉ 2024-07-13 19:29:08

Python 3.5+:

DeprecationWarning:inspect.getargspec() 自 Python 3.0 起已弃用,请使用spect.signature() 或inspect.getfullargspec()

所以以前:

func_args = inspect.getargspec(function).args

现在:

func_args = list(inspect.signature(function).parameters.keys())

测试:

'arg' in list(inspect.signature(function).parameters.keys())

假设我们有函数'function',它接受参数'arg',这将评估为 True,否则评估为 False。

来自 Python 控制台的示例:

Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32
>>> import inspect
>>> 'iterable' in list(inspect.signature(sum).parameters.keys())
True

Python 3.5+:

DeprecationWarning: inspect.getargspec() is deprecated since Python 3.0, use inspect.signature() or inspect.getfullargspec()

So previously:

func_args = inspect.getargspec(function).args

Now:

func_args = list(inspect.signature(function).parameters.keys())

To test:

'arg' in list(inspect.signature(function).parameters.keys())

Given that we have function 'function' which takes argument 'arg', this will evaluate as True, otherwise as False.

Example from the Python console:

Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32
>>> import inspect
>>> 'iterable' in list(inspect.signature(sum).parameters.keys())
True
临风闻羌笛 2024-07-13 19:29:08

我认为您正在寻找的是当地人的方法 -

In [6]: def test(a, b):print locals()
   ...: 

In [7]: test(1,2)              
{'a': 1, 'b': 2}

I think what you're looking for is the locals method -

In [6]: def test(a, b):print locals()
   ...: 

In [7]: test(1,2)              
{'a': 1, 'b': 2}
猫性小仙女 2024-07-13 19:29:08

我认为使用装饰器可以满足您的需求。

class LogWrappedFunction(object):
    def __init__(self, function):
        self.function = function

    def logAndCall(self, *arguments, **namedArguments):
        print "Calling %s with arguments %s and named arguments %s" %\
                      (self.function.func_name, arguments, namedArguments)
        self.function.__call__(*arguments, **namedArguments)

def logwrap(function):
    return LogWrappedFunction(function).logAndCall

@logwrap
def doSomething(spam, eggs, foo, bar):
    print "Doing something totally awesome with %s and %s." % (spam, eggs)


doSomething("beans","rice", foo="wiggity", bar="wack")

运行它,它将产生以下输出:

C:\scripts>python decoratorExample.py
Calling doSomething with arguments ('beans', 'rice') and named arguments {'foo':
 'wiggity', 'bar': 'wack'}
Doing something totally awesome with beans and rice.

Here is something I think will work for what you want, using a decorator.

class LogWrappedFunction(object):
    def __init__(self, function):
        self.function = function

    def logAndCall(self, *arguments, **namedArguments):
        print "Calling %s with arguments %s and named arguments %s" %\
                      (self.function.func_name, arguments, namedArguments)
        self.function.__call__(*arguments, **namedArguments)

def logwrap(function):
    return LogWrappedFunction(function).logAndCall

@logwrap
def doSomething(spam, eggs, foo, bar):
    print "Doing something totally awesome with %s and %s." % (spam, eggs)


doSomething("beans","rice", foo="wiggity", bar="wack")

Run it, it will yield the following output:

C:\scripts>python decoratorExample.py
Calling doSomething with arguments ('beans', 'rice') and named arguments {'foo':
 'wiggity', 'bar': 'wack'}
Doing something totally awesome with beans and rice.
攒一口袋星星 2024-07-13 19:29:08

在具有 Signature 对象的 Python 3.+ 中,获取参数名称到值之间的映射的一种简单方法是使用 Signature 的 bind() 方法!

例如,这是一个用于打印这样的地图的装饰器:

import inspect

def decorator(f):
    def wrapper(*args, **kwargs):
        bound_args = inspect.signature(f).bind(*args, **kwargs)
        bound_args.apply_defaults()
        print(dict(bound_args.arguments))

        return f(*args, **kwargs)

    return wrapper

@decorator
def foo(x, y, param_with_default="bars", **kwargs):
    pass

foo(1, 2, extra="baz")
# This will print: {'kwargs': {'extra': 'baz'}, 'param_with_default': 'bars', 'y': 2, 'x': 1}

In Python 3.+ with the Signature object at hand, an easy way to get a mapping between argument names to values, is using the Signature's bind() method!

For example, here is a decorator for printing a map like that:

import inspect

def decorator(f):
    def wrapper(*args, **kwargs):
        bound_args = inspect.signature(f).bind(*args, **kwargs)
        bound_args.apply_defaults()
        print(dict(bound_args.arguments))

        return f(*args, **kwargs)

    return wrapper

@decorator
def foo(x, y, param_with_default="bars", **kwargs):
    pass

foo(1, 2, extra="baz")
# This will print: {'kwargs': {'extra': 'baz'}, 'param_with_default': 'bars', 'y': 2, 'x': 1}
场罚期间 2024-07-13 19:29:08

这是另一种无需使用任何模块即可获取函数参数的方法。

def get_parameters(func):
    keys = func.__code__.co_varnames[:func.__code__.co_argcount][::-1]
    sorter = {j: i for i, j in enumerate(keys[::-1])} 
    values = func.__defaults__[::-1]
    kwargs = {i: j for i, j in zip(keys, values)}
    sorted_args = tuple(
        sorted([i for i in keys if i not in kwargs], key=sorter.get)
    )
    sorted_kwargs = {
        i: kwargs[i] for i in sorted(kwargs.keys(), key=sorter.get)
    }   
    return sorted_args, sorted_kwargs


def f(a, b, c="hello", d="world"): var = a
    

print(get_parameters(f))

输出:

(('a', 'b'), {'c': 'hello', 'd': 'world'})

Here is another way to get the function parameters without using any module.

def get_parameters(func):
    keys = func.__code__.co_varnames[:func.__code__.co_argcount][::-1]
    sorter = {j: i for i, j in enumerate(keys[::-1])} 
    values = func.__defaults__[::-1]
    kwargs = {i: j for i, j in zip(keys, values)}
    sorted_args = tuple(
        sorted([i for i in keys if i not in kwargs], key=sorter.get)
    )
    sorted_kwargs = {
        i: kwargs[i] for i in sorted(kwargs.keys(), key=sorter.get)
    }   
    return sorted_args, sorted_kwargs


def f(a, b, c="hello", d="world"): var = a
    

print(get_parameters(f))

Output:

(('a', 'b'), {'c': 'hello', 'd': 'world'})
千寻… 2024-07-13 19:29:08

inspect.signature 非常慢。 最快的方法是

def f(a, b=1, *args, c, d=1, **kwargs):
   pass

f_code = f.__code__
f_code.co_varnames[:f_code.co_argcount + f_code.co_kwonlyargcount]  # ('a', 'b', 'c', 'd')

inspect.signature is very slow. Fastest way is

def f(a, b=1, *args, c, d=1, **kwargs):
   pass

f_code = f.__code__
f_code.co_varnames[:f_code.co_argcount + f_code.co_kwonlyargcount]  # ('a', 'b', 'c', 'd')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文