Is it explicitly specified?
mario at ruggier.org
Sun Feb 3 16:41:43 CET 2008
On Feb 3, 4:19 pm, Steve Holden <st... at holdenweb.com> 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.
I think you are all very much on target, and not drifted at all. The
real issue with this situation is None, as there is ambiguity there,
whether a caller wants something to be None or whtehr it is simpy None
because it is not set...
My use case is that I have an object that is part of a collection. The
collection sets what the preferred defaults are for specific object
attributes, but individual objects may override some of these. Thus,
if an object does not set any of these attributes, then the default as
per the collection's settings is to be used.
In one case, the collection attributes a specific meaning to
attr=None, but the actual default for attr is something else. However,
if an object explicitly wants to state that his attr=None (that is a
valid value, and has specific meaning) I would like to use that as
value, but if no value is supplied for attr by the object, then I
would like to use the default value from the collection.
More information about the Python-list