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

后端 未结 4 1820
再見小時候
再見小時候 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 13:52

    It's a deliberate feature. If the python code you run evaluates to exactly None then it is not displayed.

    This is useful a lot of the time. For example, calling a function with a side effect may be useful, and such functions actually return None but you don't usually want to see the result.

    For example, calling print() returns None, but you don't usually want to see it:

    >>> print("hello")
    hello
    >>> y = print("hello")
    hello
    >>> y
    >>> print(y)
    None
    

提交回复
热议问题