blais at furius.ca
Thu May 24 02:08:56 CEST 2007
I often have the need for a generic object to use as the default value
for a function parameter, where 'None' is a valid value for the
parameter. For example:
_sentinel = object()
def first(iterable, default=_sentinel):
"""Return the first element of the iterable, otherwise the default value (if
for elem in iterable: # thx to rhettinger for optim.
if default is _sentinel:
Here, 'default' could legally accept None, so I cannot use that as the
default value, nor anything else as far as I'm concerned (I don't know
what lives in the iterable, so why should I make assumptions?).
Sometimes in the past I've create generic objects, declared a class,
class NoDef: pass
and lately I've even started using the names of builtin functions (it
bothers me a little bit though, that I do that).
I think Python needs a builtin for this very purpose. I propose
'nodef', a unique object whose sole purpose is to serve as a default
value. It should be unique, in the same sense that 'None' is unique.
Comments or alternative solutions?
More information about the Python-Dev