'None' is not displayed as I expected in Python interactive mode

后端 未结 4 1819
再見小時候
再見小時候 2020-12-06 13:14

I thought the display in Python interactive mode was always equivalent to print(repr()), but this is not so for None. Is this a language feature or

4条回答
  •  青春惊慌失措
    2020-12-06 14:08

    None represents the absence of a value, but that absence can be observed. Because it represents something in Python, its __repr__ cannot possibly return nothing; None is not nothing.

    The outcome is deliberate. If for example a function returns None (similar to having no return statement), the return value of a call to such function does not get shown in the console, so for example print(None) does not print None twice, as the function print equally returns None.

    On a side note, print(repr()) will raise a TypeError in Python.

提交回复
热议问题