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

Don Spaulding donspauldingii at gmail.com
Fri Jul 15 16:05:11 CEST 2011


On Wed, Jul 13, 2011 at 8:23 PM, Steven D'Aprano <steve at pearwood.info>wrote:

> Eric Snow wrote:
>
>  A New Solution
>> ---------------
>>
>> Provide a builtin version of a Deferred singleton, like None or
>> NotImplemented.  When resolving arguments for a call, if an argument
>> is this Deferred object, replace it with the default argument for that
>> parameter on the called function, if there is one.  If there isn't,
>> act as though that argument was not passed at all.
>>
>
> I don't believe that should be a public object. If it's public, people will
> say "Yes, but what do you do if you want the default to actually be the
> Deferred singleton?" It's the None-as-sentinel problem all over again...
> sometimes you want None to stand in for no value, and sometimes you want it
> to be a first class value.


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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110715/402cef47/attachment.html>


More information about the Python-ideas mailing list