Is it explicitly specified?

Mel mwilson at the-wire.com
Sun Feb 3 18:31:46 EST 2008


Steve Holden wrote:
> Diez B. Roggisch wrote:
>> Bjoern Schliessmann schrieb:
>>> 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?
>>
>> Any situation in which there would otherwise lots of documentation 
>> needed to inform the user that the sentinel value is something else 
>> than None.
>>
>> Take something like this as an example:
>>
>> def update_user(some, values, nullable_value=sentinel):
>>      # first, work with some and values
>>      ...
>>      # then, on something actually being passed to nullable_value
>>      if nullable_value is not sentinel:
>>         connection.cursor().execute("update table set value = ?", 
>> nullable_value)
>>
>>
>>
>> I've seen this before, in code from e.g. Alex Martelli.
>>
> Sure, but the OP's question was "Is there any way to tell between 
> whether a keyword arg has been explicitly specified (to the same value 
> as the default for it)". We've drifted a long way from that, since the 
> code you demonstrate doesn't detect an explicit call with 
> nullable_value==sentinel -- the reason being, I submit, that there is no 
> use case for such code.

The code above works on the assumption that the value named sentinel 
is completely out-of-band, and no user has any reason to use the 
sentinel object in a call.  An example up-thread uses a 
uniquely-constructed instance of object for this.  I kind of like 
defining a class, because I get a docstring to explain what's going on.

	Mel.



More information about the Python-list mailing list