[Python-ideas] Python docs page: In what ways is None special
Chris Barker - NOAA Federal
chris.barker at noaa.gov
Mon Jul 23 21:43:29 EDT 2018
I agree that some more docs on the specialness of None (and, to a
lessor extent, True and False).
A few comments:
> None is a keyword
> ==============
>>>> None = 0
> SyntaxError: can't assign to keyword
One of the implications of this is that “None” will always be the
Singleton None object — so you can (and should) use:
Something is None
To test for None.
> The Command Line Interpreter hides None
> =================================
>>>> None
That’s a good one to highlight!
>
> None is false in a boolean context
> ==========================
>>>> bool(None)
> False
Maybe this belongs more in a discussion of “Falseyness”
> Procedures return None
> ==================
>>>> a = [3,1,2]
>>>> b = a.sort()
>>>> a, b
> ([1, 2, 3], None)
This is less about None than about the convention that mutating
methods return None. Maybe that discussion belongs elsewhere.
> Dictionary get returns None if not found
> ==============================
>>>> {}.get('dne') is None
> True
Belongs with dict docs really, and not really true — dict.get()
returns the default value, which is None be default.
> None is default return value
> =====================
>>>> def fn(): pass
> ...
>>>> fn() # No response!
>>>> print(fn()) # Here's why.
> None
Yup.
> None is used as a sentinel default value
> ==============================
> Particularly useful when default value must be determined
> in body of function.
> ---
> def insort_right(a, x, lo=0, hi=None):
> # ...
> if hi is None:
> hi = len(a)
> ---
This is also a convention — and primarily applies to mutable defaults,
which you hardly ever want to assign directly.
So a good example of None being used as a sentinel, but nog really
anything special about None.
-CHB
> /
More information about the Python-ideas
mailing list