有没有办法知道如果一个参数的值是默认与用户指定的? [英] Is there any way to know if the value of an argument is the default vs. user-specified?

查看:118
本文介绍了有没有办法知道如果一个参数的值是默认与用户指定的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在找的东西,就像Lisp语言的ARG提供-P的变量,以帮助区分由用户指定的值相同的默认值。

I'm looking for something that works like Lisp's arg-supplied-p variables, to help differentiate a default value from the same value specified by a user.

例如:

def foo(a=10):
    pass

我想在富知道,如果它被称为是这样的:

I'd like to know in foo if it was called like this:

foo()

或者是这样的:

foo(10)

甚至是这样的:

foo(a=10)

最后2有足够的代名词给我;我并不需要知道的细节。我已经通过了检查模块看上去有点,但getargspec所有3调用返回完全相同的结果。

The last 2 are synonymous enough to me; I don't need to know that detail. I've looked through the inspect module a bit, but getargspec returns exactly the same results for all 3 calls.

推荐答案

除了检查源$ C ​​$ C,有没有办法告诉三个函数调用外 - 他们的意味着的到具有完全相同的含义。如果你需要区分它们,使用不同的默认值,例如

Except for inspecting the source code, there is no way to tell the three function calls apart – they are meant to have exactly the same meaning. If you need to differentiate between them, use a different default value, e.g. None.

def foo(a=None):
    if a is None:
        a = 10
        # no value for a provided

这篇关于有没有办法知道如果一个参数的值是默认与用户指定的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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