Python:将函数参数的默认值传递给* args或** kwargs [英] Python: Passing the default values of function' arguments to *args or **kwargs

查看:306
本文介绍了Python:将函数参数的默认值传递给* args或** kwargs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑示例:

def decorator(func):
    def wrapper(*args, **kwargs):
        print(args, kwargs)
        func(*args, **kwargs)
    return wrapper

@decorator
def foo(x, y, z=0):
    pass

foo(5, 5)

输出:

(5, 5) {}

为什么不(5, 5) {'z': 0}?

推荐答案

包装器只是一个正常的函数.它不能访问"包装函数的内部.

The wrapper is just a normal function. It does not have "access" to the internals of the wrapped function.

您必须使用自省功能来获取它们.看到一个相关的问题:

You would have to use introspection to get them. See a related question:

如何找出Python中另一个函数中特定函数自变量的默认值?

这篇关于Python:将函数参数的默认值传递给* args或** kwargs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆