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?
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On May 23, 2007, at 8:08 PM, Martin Blais wrote:
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.
I do the same thing for 'get' calls, where None is a legal return value. I often call this unique object 'missing', e.g. missing = object() if some_dict.get('foo', missing) is missing: # It's missing I like the way that reads. Still, I'm -1 on adding something like this to built-ins because any built-in could potentially be a value in a mapping or a default argument. Have some super-secret module global instantiated just for the purpose prevents that. - -Barry -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (Darwin) iQCVAwUBRlTfBHEjvBPtnXfVAQI3kQP6AzYa1VNUIgaqY4aQAW3dUX2sxicEikts NT6NIo/F676b1P7XYCrN7RA9JYWoyJmMhqrz7EN3SL2dkzd4mcY/XZF/zbY9ph8d W1SEWo00ImFitSRwngIlUmhlcZimpIQ0Of0hCdm9uK0Cpyk03FXbUelY1LvunJ2T z8tCQzd8hOw= =R8bc -----END PGP SIGNATURE-----
Martin Blais wrote:
I don't know what lives in the iterable, so why should I make assumptions?
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.
If the aforementioned iterable can yield *anything*, then it might yield this 'nodef' value as well. For this reason, there *can't* exist any *standard* guaranteed-unambiguous sentinel value. Each use case needs its own, to ensure it's truly unambiguous in the context of that use case. -- Greg Ewing, Computer Science Dept, +--------------------------------------+ University of Canterbury, | Carpe post meridiem! | Christchurch, New Zealand | (I'm not a morning person.) | greg.ewing@canterbury.ac.nz +--------------------------------------+
From: "Greg Ewing"
If the aforementioned iterable can yield *anything*, then it might yield this 'nodef' value as well.
For this reason, there *can't* exist any *standard* guaranteed-unambiguous sentinel value. Each use case needs its own, to ensure it's truly unambiguous in the context of that use case.
Right. That's why Barry and others write: missing = object() v = d.get(k, missing) That is the guaranteed way to get a unique object. Raymond
participants (4)
-
Barry Warsaw
-
Greg Ewing
-
Martin Blais
-
Raymond Hettinger