Python中的'print'是什么类型?

3
Python 2.7.12 (default, Jul 27 2016, 16:11:41) 
[GCC 5.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> type(print)
  File "<stdin>", line 1
    type(print)
             ^
SyntaxError: invalid syntax
>>> 

在Python中,一切皆为对象,那么为什么类型(type)为print的操作会出现上述错误?
3个回答

11
在Python 2中,print是一个语句。参见 https://www.python.org/dev/peps/pep-3105/。 并非所有的都是对象。例如,if、else、for等不是。这些也是语言关键字,不能用作变量名。
然而,在Python 3中,print()是一个函数,因此它也是一个对象。

3

print简单语句 之一。它不是一个对象,因此它没有类型。同样地,returnbreak 也没有类型。


-2

这是我的终端告诉我的。

>>> type(list)
<class 'type'>
>>> type(print)
<class 'builtin_function_or_method'>
>>> 

4
你正在使用Python 3,OP使用的是Python 2.7.12。 - S. de Melo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接