Is it explicitly specified?

Arnaud Delobelle arnodel at googlemail.com
Sun Feb 3 06:31:18 EST 2008


On Feb 3, 11:00 am, mario ruggier <ma... at ruggier.org> wrote:
> Is there any way to tell between whether a keyword arg has been explicitly
> specified (to the same value as the default for it) or not... For example:
>
> def func(key=None):
>     do something with key
>
> But the following two usages give same results:
>
> func()
> func(key=None)
>
> It may sometimes be useful to make use of the conceptual difference between
> these two cases, that is that in one case the user did not specify any key
> and in the other the user explicitly specified the key to be None.
>
> Is there any way to tell this difference from within the called function?
> And if so, would there be any strong reasons to not rely on this
> difference? Would it be maybe a little abusive of what a keyword arg
> actually is?
>
> mario

One way is to create an object which is only ever used as the default
arguement:

nokey = object()

def func(key=nokey):
    if key is nokey:
        # key not given
    else:
        # key given

--
Arnaud




More information about the Python-list mailing list