Python Essentials
上QQ阅读APP看书,第一时间看更新

The None object

One very simple kind of Python object is the None object. It has few methods, and there's only a single instance of this object available. It is a handy way to identify something as missing or not applicable. It's often used as a default value for optional parameters to a function.

The None object is a singleton; there can be only one. This object is immutable: we can't change it in any way.

With the interactive use of Python, the REPL doesn't print the None object. For example, when we evaluate the print() function, the proper result of this function is always None. The side-effect of this function is to print things on our console. Looking forward to Chapter 3, Expressions and Output, we'll give this quick example of a function that returns None:

>>> a = print("hello world")
hello world
>>> a
>>> a is None
True

We've evaluated the print() function and saved the result of the print function in the a variable. The visible side-effect of printing is to see the string value displayed on the console. The result is the None object, which is not printed. We can, however, use the is comparison operator to see that the value of a really is the None object.