[Python-ideas] Python docs page: In what ways is None special

Paul Moore p.f.moore at gmail.com
Mon Jul 23 07:15:14 EDT 2018


On 23 July 2018 at 10:16, Steve Dower <steve.dower at python.org> wrote:
> On 23Jul2018 1003, Jonathan Fine wrote:
>>
>> This arises out of PEP 505 - None-aware operators.
>>
>> I thought, a page on how None is special would be nice.
>> I've not found such a page on the web. We do have
>> ===
>> https://docs.python.org/3/library/constants.html
>> None
>> The sole value of the type NoneType. None is
>> frequently used to represent the absence of a
>> value, as when default arguments are not passed
>> to a function. Assignments to None are illegal
>> and raise a SyntaxError.
>> ===
>>
>> So decided to start writing such a page, perhaps to be
>> added to the docs.  All code examples in Python3.4.
>
>
> There's also
> https://docs.python.org/3/c-api/none.html?highlight=py_none#c.Py_None
>
> "The Python None object, denoting lack of value. This object has no methods.
> It needs to be treated just like any other object with respect to reference
> counts."
>
> I don't know that documenting the behaviours of None are that interesting
> (e.g. not displaying anything at the interactive prompt), though it'd be
> perfect for a blog and/or conference talk. But if there appear to be
> behaviours that are not consistent or cannot be easily inferred from the
> existing documentation, then we should think about why that is and how we
> could enhance the documentation to ensure it accurately describes what None
> is supposed to be.
>
> That said, your examples are good :)

The examples are interesting, agreed. One thing they highlight to me
is that most uses of None are effectively convention. The only two
behaviours that are unique to None and implemented in the interpreter
are:

1. Functions that don't return an explicit value return None.
2. The interactive interpreter does not display the value of a typed
expression when that value is None.

(the fact that None is a keyword is not unique - True and False are
also keywords - although it is a distinguishing feature).

One of the key points about this proposal is that it adds a number of
additional ways in which the interpreter treats None specially (i.e.,
four dedicated operators). That elevates None from the position of
being a commonly used sentinel value to one that has language support
for being a sentinel. That's not necessarily a bad thing, but it is a
point that should be considered when reviewing this PEP (people who
prefer to use object() for their sentinels are probably less happy
about making None "special", for example - Pandas uses NaN as the
"missing data" sentinel rather than None, although Pandas' focus on
bulk processing means that dedicated operators probably wouldn't help
anyway).

Overall, my personal feeling is that I do use None specially (and a
lot of code I see does too), so having None-aware operators has some
merit. But I don't find None to be special enough to warrant quite
this *many* operators, nor do I find that the patterns that operators
like ?. and ?[ support are ones I use a lot. Actually, the ?. and ?[
operators seem like they'd be much more useful if I did more JSON
processing - but I don't, so I have little feel for whether language
support is needed here or whether better library support (stdlib
and/or 3rd party) for optional data is sufficient.

Paul


More information about the Python-ideas mailing list