print_stmt |
::= | "print" ( [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 "\n", 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.) Note: Objects which act like file objects but which are not the built-in file objects often do not properly emulate this aspect of the file object's behavior, so it is best not to rely on this.
print依次对每个表达式求值,然后把结果对象写入标准输出(见下面)。如果一个对象不是一个字符串,首先用字符串转换法则将其转成一个字符串。然后(结果的或者原来的)字符串就被写入。在每个对象被(转换和)写入以前,输出系统会先写一个空格,除非它认为当前处在一行的起首位置。这里是(起首)情形:(1)当尚未有字符写到标准输出,(2)当上一个写入标准输出的字符是"\n",或者(3)当前一次作用于标准输出上的操作不是print语句。注意:那些行为类似file对象但是并非内置file对象的对象常常不能正确地模拟file对象这方面的行为,所以最好不要依赖于此。
A "\n" character is written at the end, unless the print statement ends with a comma. This is the only action if the statement contains just the keyword print.
A "\n" character is written at the end, unless the print statement ends with a comma. This is the only action if the statement contains just the keyword print.
Standard output is defined as the file object named stdout
in the built-in module sys. If no such object exists, or if
it does not have a write() method, a RuntimeError
exception is raised.
标准输出定义为内置模块sys中名为stdout的file文件对象。如果该对象不存在,或者它没有write()方法,会抛出一个RuntimeError异常。
print also has an extendedform, defined by the second portion of the syntax described above.
This form is sometimes referred to as ``print chevron.''
In this form, the first expression after the >
>
must
evaluate to a ``file-like'' object, specifically an object that has a
write() method as described above. With this extended form,
the subsequent expressions are printed to this file object. If the
first expression evaluates to None
, then sys.stdout
is
used as the file for output.
print也有一个扩展形式,由上面描述的语法的第二部分定义。这个形式有时被称为“返身打印(print chevron)”。在该形式中,跟在"»"后的第一个表达式必须取值为一个"类文件"的对象,确切地说就是一个具有上面所描述的write()方法的对象。以这个扩展形式,后续表达式被打印到该文件对象上。如果第一个表达式取值为None,sys.stdout对象就被用来作为输出文件。