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

后端 未结 4 1817
再見小時候
再見小時候 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:19

    In Python, a function that does not return anything but is called only for its side effects actually returns None. As such functions are common enough, Python interactive interpreter does not print anything in that case. By extension, it does not print anything when the interactive expression evaluates to None, even if it is not a function call.

    If can be misleading for beginners because you have

    >>> a = 1
    >>> a
    1
    >>>
    

    but

    >>> a = None
    >>> a
    >>>
    

    but is is indeed by design

提交回复
热议问题