gpt4 book ai didi

python - 打印函数调用详细信息(参数名称和有效值)的装饰器?

转载 作者:太空狗 更新时间:2023-10-29 17:11:27 25 4
gpt4 key购买 nike

我想创建一个函数,作为另一个函数的装饰器将打印该函数调用的详细信息——参数名称和有效值。我当前的实现是这样的。

def describeFuncCall(func):
"""
Decorator to print function call details.

parameters names and effective values.
"""

def wrapper(*func_args, **func_kwargs):
print "func_code.co_varnames =", func.func_code.co_varnames
print "func_code.co_argcount =", func.func_code.co_argcount
print "func_args =", func_args
print "func_kwargs =", func_kwargs
params = []
for argNo in range(func.func_code.co_argcount):
argName = func.func_code.co_varnames[argNo]
argValue = (
func_args[argNo]
if argNo < len(func_args)
else func.func_defaults[argNo - func.func_code.co_argcount]
)
params.append((argName, argValue))
for argName, argValue in func_kwargs.items():
params.append((argName, argValue))
params = [argName + " = " + repr(argValue)
for argName, argValue in params]
print (func.__name__ + " ( " + ", ".join(params) + " )")
return func(*func_args, **func_kwargs)

return wrapper


@describeFuncCall
def test(a, b=4, c="blah-blah", *args, **kwargs):
pass


test(1)
# test(1, 3)
# test(1, d = 5)
test(1, 2, 3, 4, 5, d=6, g=12.9)

有点效果,但有一些错误:

打电话

测试(1、2、3、4、5、d = 6、g = 12.9)

它打印

测试(a = 1,b = 2,c = 3,d = 6,g = 12.9)

预期的结果是

测试 (a = 1, b = 2, c = 3, args = [4, 5], kwargs = {'d': 6, 'g': 12.9} )

我卡在这里了。你能帮我找到合适的解决方案吗?

最佳答案

这是 Python 3.6+ 的更新版本

import inspect


def dump_args(func):
"""
Decorator to print function call details.

This includes parameters names and effective values.
"""

def wrapper(*args, **kwargs):
func_args = inspect.signature(func).bind(*args, **kwargs).arguments
func_args_str = ", ".join(map("{0[0]} = {0[1]!r}".format, func_args.items()))
print(f"{func.__module__}.{func.__qualname__} ( {func_args_str} )")
return func(*args, **kwargs)

return wrapper


@dump_args
def test(a, b=4, c="blah-blah", *args, **kwargs):
pass


test(1)
test(1, 3)
test(1, d=5)
test(1, 2, 3, 4, 5, d=6, g=12.9)

旧版本


具有默认值的工作版本:

def dumpArgs(func):
'''Decorator to print function call details - parameters names and effective values'''
def wrapper(*func_args, **func_kwargs):
arg_names = func.func_code.co_varnames[:func.func_code.co_argcount]
args = func_args[:len(arg_names)]
defaults = func.func_defaults or ()
args = args + defaults[len(defaults) - (func.func_code.co_argcount - len(args)):]
params = zip(arg_names, args)
args = func_args[len(arg_names):]
if args: params.append(('args', args))
if func_kwargs: params.append(('kwargs', func_kwargs))
print func.func_name + ' (' + ', '.join('%s = %r' % p for p in params) + ' )'
return func(*func_args, **func_kwargs)
return wrapper

@dumpArgs
def test(a, b = 4, c = 'blah-blah', *args, **kwargs):
pass

test(1)
test(1, 3)
test(1, d = 5)
test(1, 2, 3, 4, 5, d = 6, g = 12.9)

结果:

>>> test  (  a = 1, b = 4, c = 'blah-blah' )
test ( a = 1, b = 3, c = 'blah-blah' )
test ( a = 1, b = 4, c = 'blah-blah', kwargs = {'d': 5} )
test ( a = 1, b = 2, c = 3, args = (4, 5), kwargs = {'d': 6, 'g': 12.9} )

关于python - 打印函数调用详细信息(参数名称和有效值)的装饰器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6200270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com