简体   繁体   English

如何获取传递给 Python 中 function 的参数的参数名称?

[英]How to get the parameter name of an argument passing into a function in Python?

I'm making a decorator that prints out the input and output values of a function when it is called.我正在制作一个装饰器,它在被调用时打印出 function 的输入和 output 值。 I wonder how I can get the parameter name of an argument passing into the function.我想知道如何获取传递给 function 的参数的参数名称。 In the sample function below, I expect that the first argument - 5, will be of the parameter 'multiplier' and the rest will be of '*numbers', but how can I transfer those information into the decorator, because the f function in the decorator only have **args and **kwargs as parameter, not the actual parameter name of the function? In the sample function below, I expect that the first argument - 5, will be of the parameter 'multiplier' and the rest will be of '*numbers', but how can I transfer those information into the decorator, because the f function in装饰器只有 **args 和 **kwargs 作为参数,而不是 function 的实际参数名称? Hope you guys can help me, thanks a lot.希望大家能帮帮我,万分感谢。

Here is the decorator:这是装饰器:

def log(with_input=True):
    def inner(f):
        def wrapper(*args, **kwargs):
            rv = f(*args, **kwargs)
            print('*' * 40)
            print("function: ", f.__name__,inspect.signature(f))
            if with_input:
                print("input: ")
                for arg in args:
                    print('  param_name',arg)
            print("output:", rv)
            return rv
        return wrapper
    return inner

This is the sample code to test the decorator:这是测试装饰器的示例代码:

@log(with_input=True)
def multiplier_sum(multiplier,*numbers):
   sum = 0
   for n in numbers:
       sum += n
   return sum * multiplier

multiplier_sum(5,2,3,4) 

This is the output, where the parameter name associated with the argument is what I'm expected to in place of param_name:这是 output,其中与参数关联的参数名称是我期望代替 param_name 的名称:

****************************************
function:  multiplier_sum (multiplier, *numbers)
input: 
  param_name 5
  param_name 2
  param_name 3
  param_name 4
output: 45

Cool question.很酷的问题。 Here's a solution:这是一个解决方案:

import inspect

def my_decorator(func):
    def wrapper(*args, **kwargs):
        print("signature: ", inspect.signature(func))
        bound_sig = inspect.signature(func).bind(*args, **kwargs)
        print("bound signature: ", bound_sig)
        print("arguments: ", bound_sig.arguments)
        func(*args, **kwargs)
    return wrapper

@my_decorator
def f(x):
    print (f"we're in foo now, and x is {x}")

f(5)

results in:结果是:

signature:  (x)
bound signature:  <BoundArguments (x=5)>
arguments:  OrderedDict([('x', 5)])
we' re in foo now, and x is 5
function.__code__.co_varnames

This will give the parameter names.这将给出参数名称。

function.__code__.co_argcount

This will give the argument count.这将给出参数计数。

or或者

from inspect import getargspec
print getargspec(function)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM