Why does using from __future__ import print_function breaks Python2-style print?
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 3
--
Chapters
00:00 Why Does Using From __future__ Import Print_function Breaks Python2-Style Print?
00:30 Accepted Answer Score 263
01:36 Thank you
--
Full question
https://stackoverflow.com/questions/3203...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #python27 #loops #vim #pythonimport
#avk47
ACCEPTED ANSWER
Score 263
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.)