Reference

Steven D'Aprano steve at pearwood.info
Tue Mar 4 05:38:05 CET 2014


On Mon, 03 Mar 2014 18:02:04 -0500, Roy Smith wrote:

> In article <mailman.7669.1393885090.18130.python-list at python.org>,
>  Ben Finney <ben+python at benfinney.id.au> wrote:
> 
>> That's right. Python provides this singleton and then recommends you
>> compare with ‘is’, precisely to protect against pathological cases like
>> a “return True when compared for equality with None” data type.
> 
> Going off on a tangent, I've often wished Python provided more kinds of
> None-ness.  I'll often write:
> 
> def f(arg=None):
>    whatever
> 
> where it would be nice to differentiate between "this was called with no
> arguments" and "this was called with an argument of None".  Sure, I can
> work around that with things like **kwargs,


That's the hard way. The easy way is:

_SENTINEL = object()

def f(arg=_SENTINEL):
    if arg is _SENTINEL:
        ...


If you really cared, you could implement your own None_ish_Type and 
create as many named sentinels as you wish.


-- 
Steven



More information about the Python-list mailing list