[Zope3-dev] default and required analysis
Steve Alexander
steve@cat-box.net
Wed, 08 Jan 2003 16:10:37 +0000
> In Python, missingness is frequently signaled by using None,
Only when None is not a valid value for what is missing.
None is a convenient marker object.
Everywhere None is used like this, a marker could also be used:
_marker = object()
None is a convenient marker for when you know that None is not a
possible value, and you want to allow callers of a method to be able to
use that method argument, and also get the same behaviour as the missing
value.
So:
def grok(alpha, beta, gamma=None):
if gamma is None:
gamma = 4711 # default value for gamma
However, if gamma could legitimately be None, you can do this:
_marker = object()
def grok(alpha, beta, gamma=_marker):
if gamma is _marker:
gamma = 4177 # default value for gamma
If you want to allow callers of 'grok' to get the default value, you
must either make the default value known by other means, or make the
marker a public member of the module, so it can be imported and used by
callers.
So, I am strongly in favour of not using 'None' as the universal
'missing' value.
Incidentally, this is important for configuring adapters in Zope, where
'None' has a different meaning than 'user entered no data' or 'caller
specified no data'.
> I would call YAGNI on 'existence'; let's assume that if a field is
> specificed in a schema, the objects implementing this schema *must*
> have such a field otherwise they are in error.
I agree with this. You can use a schema, and a derived schema with the
extra field.
--
Steve Alexander