1

The other day I accidentally wrote

print("a function?")

in my Python 2.7.11 console and was quiet astonished that it would work instead of throwing an error. I assumed, there was an implicit

from __future__ import print_function

and tried

print "also a statement???"

which also worked! Note that, when importing from __future__ the statement is disabled. It is in fact disabled, and only the function sytax works if I import print_function

I couldn't find anything in the documentation, the Python docs still read:

Note: This function is not normally available as a built-in since the 
name print is recognized as the print statement. To disable the 
statement and use the print() function, use this future statement at 
the top of your module: (...)

What did I miss? Why is print a statement and a builtin function in Python 2.7?

Wolf
  • 9,679
  • 7
  • 62
  • 108
steffen
  • 8,572
  • 11
  • 52
  • 90
  • 2
    `("a function?")` is just the string `"a function?"` with brackets around it. You're just putting unnecessary brackets around your argument to the `print` statement. – khelwood May 03 '16 at 09:57
  • print is actually a special statement, not a function. – Nishant Singh May 03 '16 at 09:57
  • I stumble upon this once in a while... I will never get used to the triple meaning of parentheses O_o – steffen May 03 '16 at 10:13

1 Answers1

7

Note that the string "a function?" is an expression in Python, and an expression in parentheses is also an expression.

So your command was print ("a function?"), just printing an expression.

That is convenient for writing a line that works in both Python 2.x and in Python 3.x. The book "Python Crash Course" uses this to show code that works in both versions of Python.

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50