Is it explicitly specified?
tjreedy at udel.edu
Mon Feb 4 00:06:50 CET 2008
"mario" <mario at ruggier.org> wrote in message
news:9d3145c0-1128-4f7e-a9b5-2789f9231f34 at 1g2000hsl.googlegroups.com...
| 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.
In Python, a function parameter default value, in the narrow sense, is a
value (object) that you can and do calculate (construct) *when the function
is defined* (if necessary, with a helper function). If the 'default value'
cannot be calculated until later, then, for function definition purposes,
you only have a 'default behavior'.
If you have multiple optional keywords with default behaviors when they are
not specified, the usually mechanism is the **keywords mechanism. If you
have just one, that may seem like overkill and some people prefer the
pseudo-default or sentinal value approach given by Arnaud. The only change
I would make from his post is to add at least an underscore to the sentinal
name to indicate that it is private to the module and not for use of
importers (and excluded from 'import *').
More information about the Python-list