[Python-ideas] None-as-sentinel alternatives? (was: deferred default arguments)
Nick Coghlan
ncoghlan at gmail.com
Sat Jul 16 10:26:29 CEST 2011
On Sat, Jul 16, 2011 at 12:05 AM, Don Spaulding
<donspauldingii at gmail.com> wrote:
> When I come across a situation where I want a sentinel that doesn't preclude
> None from being a valid value, I've always spelled it like this:
> SENTINEL = object() # Guaranteed to be unique and not None.
>
> def f(arg=SENTINEL):
> if arg is SENTINEL:
> arg = 'foo'
> x(arg)
> It's always worked well, but that little voice in my head tells me there's
> probably something I'm not thinking of that will come back to bite me in the
> future. I don't think this helps the OP in any meaningful way, but I was
> wondering if you bright people might shed some light on the drawbacks of
> this approach for me. What are the better alternatives?
Explicit sentinel objects *are* the better alternative :)
As they rely on object identity, they typically don't play nicely with
serialisation, though. You need a value based sentinel for that use
case.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas
mailing list