What is print in Python?

I understand what print does, but from the fact that "type" is an element of the language? I think this is a feature, but why does it fail?

 >>> print print SyntaxError: invalid syntax 

Not a print function? Shouldn't it print something like this?

 >>> print print <function print at ...> 
+61
python
Aug 11 '11 at 3:12
source share
5 answers

In 2.7 and below, print is a statement. In python 3, the print function is a function. To use the print function in Python 2.6 or 2.7, you can do

 >>> from __future__ import print_function >>> print(print) <built-in function print> 

See this section of the Python Language Reference, as well as PEP 3105 , for why it has changed.

+63
Aug 11 '11 at 3:14
source share

Python 3 print() has a built-in function (object)

Prior to this print was a statement. Demonstration...

Python 2.x:

 % 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<----- 

Python 3.x:

 % 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. 
+34
Aug 11 2018-11-11T00:
source share

print is a bug fixed in Python 3. In Python 3, this is a function. In Python 1.x and 2.x, this is not a function, it is a special form like if or while , but unlike the two, it is not a control structure.

So, I think the most accurate thing to call this statement.

+21
Aug 11 '11 at 3:25
source share

In Python, all statements (except assignment) are expressed in reserved words, not addressable objects. That is why you cannot just print print , and you get a SyntaxError to try. This is a reserved word, not an object.

Confusingly, you may have a variable called print . You cannot access it in the usual way, but you can setattr(locals(), 'print', somevalue) , and then print locals()['print'] .

Other reserved words that may be desirable as variable names, but verboten nonetheless:

 class import return raise except try pass lambda 
+7
Aug 11 2018-11-11T00:
source share

In Python 2, print is an operator that is a completely different thing from a variable or function. Expressions are not Python objects that can be passed to type() ; they are just part of the language itself, even more than built-in functions. For example, you can do sum = 5 (even if you don’t), but you cannot do print = 5 or if = 7 , because print and if are operators.

In Python 3, the print statement was replaced by the print() function. Therefore, if you do type(print) , it will return <class 'builtin_function_or_method'> .

BONUS:

In Python 2.6+, you can put from __future__ import print_function at the top of your script (as the first line of code), and the print statement will be replaced by the print() function.

 >>> # Python 2 >>> from __future__ import print_function >>> type(print) <type 'builtin_function_or_method'> 
+2
Aug 13 '15 at 8:43
source share



All Articles