[Python-ideas] Python docs page: In what ways is None special
Jonathan Fine
jfine2358 at gmail.com
Mon Jul 23 05:03:10 EDT 2018
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.
Here's my first attempt. I'm sure I've missed some
important examples. Please help, if you can.
None is a keyword
==============
>>> None = 0
SyntaxError: can't assign to keyword
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class',
'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for',
'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal',
'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
The Command Line Interpreter hides None
=================================
>>> None
>>>
None is false in a boolean context
==========================
>>> bool(None)
False
Procedures return None
==================
>>> a = [3,1,2]
>>> b = a.sort()
>>> a, b
([1, 2, 3], None)
Dictionary get returns None if not found
==============================
>>> {}.get('dne') is None
True
None is default return value
=====================
>>> def fn(): pass
...
>>> fn() # No response!
>>> print(fn()) # Here's why.
None
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)
---
Thank you for your attention. What have I missed?
--
Jonathan
More information about the Python-ideas
mailing list