[Tutor] Increase performance of the script

Steven D'Aprano steve at pearwood.info
Wed Dec 12 03:22:14 EST 2018


On Wed, Dec 12, 2018 at 12:52:09AM -0500, Avi Gross wrote:
> Asad,
> 
> I wonder if an import from __future__ happened, perhaps in the version of
> collections you used. Later versions of 2.x allow optional use of the 3.x
> style of print.


The effect of __future__ imports, like any other import, is only within 
the module that actually does the import. Even in the unlikely event 
that collections did such a future import, it would only effect print 
within that module, not globally or in the interactive interpreter.

Here's a demo:

# prfunc_demo.py
from __future__ import print_function

try:
    exec("print 123")
except SyntaxError:
    print("old style print failed, as expected")
    print("as print is now a function")



And importing it into the interactive interpreter shows that the effect 
of the future import is localised:

[steve at ando ~]$ python2.6
Python 2.6.7 (r267:88850, Mar 10 2012, 12:32:58)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
py> import prfunc_demo
old style print failed, as expected
as print is now a function
py> print "But here in the REPL, nothing has changed."
But here in the REPL, nothing has changed.







-- 
Steve


More information about the Tutor mailing list