Hi
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
specified).
"""
for elem in iterable: # thx to rhettinger for optim.
return elem
if default is _sentinel:
raise StopIteration
return default
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,
e.g.:
class NoDef: pass
def foo(default=NoDef):
...
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?
_______________________________________________
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com