Is it explicitly specified?

Steve Holden steve at holdenweb.com
Sun Feb 3 09:32:47 EST 2008


Bjoern Schliessmann wrote:
> mario ruggier wrote:
> 
>> 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.
> 
> Do you have an example where this might be useful?
>  
Yes, a use case *would* be interesting.

>> 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? 
> 
> IMHO, it's "magic".
> 
>> Would it be maybe a little abusive of what a keyword arg actually
>> is? 
> 
> No, but you'd have to explain to users why it behaves like this. I
> wouldn't expect this behaviour. If the function signature says the
> default value for "key" is None, I expect it to behave the same no
> matter if I leave out "key" or if I set it explicitly.
> 
Well, quite: the whole point of a default is to provide a value to be 
used when the caller doesn't explicitly provide one. The use of a 
sentinel object() instance is the only sane way to perform this ever so 
slightly insane task. If the argument value isn't the sentinel then the 
function has to set it to the required default.

But this isn't the strict requirement as stated by the OP, of course, 
who wants to be able to explicitly detect when the sentinel object is 
passed in as an actual keyword (or positional) argument. I don't believe 
that's possible without large amounts of heavyweight introspection.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list