Why does using "from __future__ import print_function" break Python2-style print?
Asked Answered
A

1

163

I tried this code in Python 2.7:

from __future__ import print_function
import sys, os, time

for x in range(0,10):
    print x, sep=' ', end=''
    time.sleep(1)

But I get an error that says:

$ python2 xy.py
  File "xy.py", line 5
    print x, sep=' ', end=''
          ^
SyntaxError: invalid syntax
$

I thought that using the __future__ import should make it possible to use sep and end in a print statement, but now it apparently doesn't work at all.

Why not?

Anticline answered 16/8, 2015 at 7:7 Comment(6)
You imported print as a function but you're still treating it as a statement.Sthenic
you can't call print without the parenthesis because you have changed print to be a function print(args)Ludivinaludlew
See also What is __future__ in Python used for and how/when to use it, and how it worksIndeciduous
You are still using python 2 print syntax, but you imported the print function from the future (aka the print function inside python 3). It replaces the old print syntax with the new syntax, thus creating an errorHilbert
Also, I don't know why you are still using python 2, when python 3 is out, but whatever.Hilbert
Voting to reopen because the prior popularity of the question shows that there's a legitimate misconception underlying the error in the code. I edited to highlight what appeared to be the original source of confusion and the original reasoning.Euonymus
S
260

The whole point of from __future__ import print_function is to bring the print function from Python 3 into Python 2.6+. Thus, it must be used like a function here:

from __future__ import print_function

import sys, os, time

for x in range(0,10):
    print(x, sep=' ', end='')  # No need for sep here, but okay :)
    time.sleep(1)

__future__ statements change fundamental things about the language. From the documentation:

A future statement is recognized and treated specially at compile time: Changes to the semantics of core constructs are often implemented by generating different code. It may even be the case that a new feature introduces new incompatible syntax (such as a new reserved word), in which case the compiler may need to parse the module differently. Such decisions cannot be pushed off until runtime.

(For the same reason, they must also appear first in the source code, before any other imports. The only things that can precede a __future__ statement are the module docstring, comments, blank lines, and other future statements.)

Subcontinent answered 16/8, 2015 at 7:11 Comment(9)
Thanks... However, now it is printing 0123456789 instead of 0 1 2 3 4 5 6 7 8 9. how do I solve that?Anticline
@UHMIS, do end=' '.Subcontinent
@UHMIS, it's not useless, it's just not useful in that particular code, because you're only printing one thing per call to print. If you were doing, for example, print(x, x**2, sep=' ', end=' '), it would be useful there because it's putting a separator between each item (in this case, x and x**2). Of course, the default sep is ' ', so you don't have to specify that anyway.Subcontinent
@AvinashRaj, can you show me what you mean?Subcontinent
@AvinashRaj, let's talk about your answer in the comments to your answer, to make it easier for posterity :).Subcontinent
@Subcontinent then why he says that my code is working?Imprisonment
@AvinashRaj, I don't know; you'd have to ask UHMIS. But as I said in a comment to your answer, perhaps OP made a change and didn't mention it. And OP's first comment was that there was still an error.Subcontinent
@not2qubit I reverted the edit you just made, and I wanted to let you know why, since I know it was done in good faith. While the table was obviously related to __future__, it didn't really help answer the question or directly expand on the answer; it was just some extraneous related info. Also I'm not sure what exactly your intention was behind italicizing "docstring, comments, blank lines". Please feel free to respond if you want to discuss it, or if you'd like to comment with that link to the __future__ docs. Thanks for wanting to improve my answer.Subcontinent
@Subcontinent No problem. I just wanted to show what other features was available, along with when they were introduced, since that would be a natural followup question and would have helped clarify what they are used for.Arterial

© 2022 - 2024 — McMap. All rights reserved.