官术网_书友最值得收藏!

  • Python Essentials
  • Steven F. Lott
  • 213字
  • 2021-07-16 13:53:10

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.

主站蜘蛛池模板: 高要市| 遂川县| 东安县| 黔西县| 五华县| 康平县| 旬阳县| 昭通市| 台北市| 甘洛县| 泸溪县| 襄垣县| 嵩明县| 万荣县| 莱州市| 行唐县| 龙游县| 吉隆县| 红桥区| 偃师市| 都江堰市| 海盐县| 綦江县| 沁源县| 靖宇县| 石狮市| 邵武市| 桦南县| 灌南县| 界首市| 家居| 东丽区| 兰州市| 南漳县| 五大连池市| 界首市| 莫力| 台湾省| 东乡| 托克托县| 沁源县|