Re: [Python-ideas] None-as-sentinel alternatives? (was: deferred default arguments)

On Wed, Jul 13, 2011 at 8:23 PM, Steven D'Aprano <steve@pearwood.info>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?

On Fri, Jul 15, 2011 at 10:05 AM, Don Spaulding <donspauldingii@gmail.com> wrote:
No, this approach isn't error-prone. For non-idea questions you might consider comp.lang.python in the future. =) Mike

On Sat, Jul 16, 2011 at 12:05 AM, Don Spaulding <donspauldingii@gmail.com> wrote:
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@gmail.com | Brisbane, Australia

On Fri, Jul 15, 2011 at 10:05 AM, Don Spaulding <donspauldingii@gmail.com> wrote:
No, this approach isn't error-prone. For non-idea questions you might consider comp.lang.python in the future. =) Mike

On Sat, Jul 16, 2011 at 12:05 AM, Don Spaulding <donspauldingii@gmail.com> wrote:
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@gmail.com | Brisbane, Australia
participants (3)
-
Don Spaulding
-
Mike Graham
-
Nick Coghlan