On Feb 3, 12:35 pm, TeroV <[EMAIL PROTECTED]> wrote: > Jorge Godoy wrote: > > mario ruggier 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? > > > If you change the idiom you use to: > > >>>> def myfunc(**kwargs): > > ... something = kwargs.get('something', None) > > ... print something > > ... > >>>> myfunc() > > None > >>>> myfunc(something='Something') > > Something > > > Then you can test if 'something' is in kwargs dict or not. If it isn't, > > then you used the default value. If it is, then the user > > supplied 'something' to you, no matter what its value is. > > Exactly, and if you use idiom func(*args, **kwargs) you can distinguish > all the usage cases: > > >>> def func(*args, **kwargs): > ... print(args, kwargs) > ... > >>> func() > () {} > >>> func(key='alabama') > () {'key': 'alabama'} > >>> func('alabama') > ('alabama',) {}
Nice... but I would still like to be able to specify the key's default value in the func signature, and in this case this would not be possible: >>> def func(key=None, *args, **kwargs): ... print(key, args, kwargs) ... >>> func() (None, (), {}) >>> func(None) (None, (), {}) >>> func(key=None) (None, (), {}) >>> I would still need an additional object, as Arnaud suggests. mario -- http://mail.python.org/mailman/listinfo/python-list