为什么一个函数的类型是<class 'NoneType'>?

Why is the type of a function <class 'NoneType'>?

运行之后是下面的代码。我观察到 print() 的 return 类型是 None。请解释为什么会这样?

>>> v = print(7)                                                                                                                     >>> v                                                                                                                   >>> type(v)                                                                                                             <class 'NoneType'>                                                                                                      >>>  
>>> type(print(y))
7
<class 'NoneType'>
>>> type(None)
<class 'NoneType'>

您需要使用:

type(print)

如果添加括号,它会调用函数,type() 将为您提供 return 值的类型

如果函数不 return 任何东西,它 returns None in python.

您将 print 与 return 混淆了。打印函数的工作只是将值转换为字符串并打印,它没有 return 任何东西,所以它是 'NoneType'.

更详细的解释可以参考