我理解是什么print,但语言元素的"类型"是什么?我认为这是一个功能,但为什么会失败?
>>> print print
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
不是print功能吗?不应该打印这样的东西吗?
>>> print print
<function print at ...>
Run Code Online (Sandbox Code Playgroud)
wim*_*wim 63
在2.7及以下,print是一个声明.在python 3中,print是一个函数.要在Python 2.6或2.7中使用print函数,您可以这样做
>>> from __future__ import print_function
>>> print(print)
<built-in function print>
Run Code Online (Sandbox Code Playgroud)
请参阅Python语言参考中的本节以及PEP 3105,了解其变更原因.
Joh*_*web 35
在Python 3中,print()是一个内置函数(对象)
在此之前,print是一个声明.示范...
% pydoc2.6 print
The ``print`` statement
***********************
print_stmt ::= "print" ([expression ("," expression)* [","]]
| ">>" expression [("," expression)+ [","]])
``print`` evaluates each expression in turn and writes the resulting
object to standard output (see below). If an object is not a string,
it is first converted to a string using the rules for string
conversions. The (resulting or original) string is then written. A
space is written before each object is (converted and) written, unless
the output system believes it is positioned at the beginning of a
line. This is the case (1) when no characters have yet been written
to standard output, (2) when the last character written to standard
output is a whitespace character except ``' '``, or (3) when the last
write operation on standard output was not a ``print`` statement. (In
some cases it may be functional to write an empty string to standard
output for this reason.)
-----8<-----
Run Code Online (Sandbox Code Playgroud)
% pydoc3.1 print
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
Run Code Online (Sandbox Code Playgroud)
Omn*_*ous 21
print是一个在Python 3中已经纠正的错误.在Python 3中它是一个函数.在Python 1.x和2.x中它不是一个函数,它是一个特殊的形式,if或者while,但与这两个不同,它不是一个控制结构.
所以,我想最准确的说法是声明.
在Python中,所有语句(赋值除外)都用保留字表示,而不是可寻址对象.这就是为什么你不能简单地print print让你得到一个SyntaxError尝试.这是一个保留词,而不是一个对象.
令人困惑的是,您可以拥有一个名为的变量print.你不能以正常的方式解决它,但你可以setattr(locals(), 'print', somevalue),然后print locals()['print'].
其他保留字可能作为变量名称但仍然是verboten:
class
import
return
raise
except
try
pass
lambda
Run Code Online (Sandbox Code Playgroud)