Odd Behavior in Python 2.7

I was tinkering around with replacing the print statement with the print function in a Python 2 script when I ran across this peculiar oddity.

>>> from __future__ import print_function as print
  File "<stdin>", line 1
    from __future__ import print_function as print
SyntaxError: invalid syntax
>>> from __future__ import print_function as print_
>>> from __future__ import print_function as print

Notice that sequence is just importing the future print function as different names in each iteration.  The oddity is that the first import fails but the third (which is exactly the same) succeeds after performing the second.
** Note: I don’t know if replacing print with the print from the futures module is a wise thing to do.  I was simply using it while trying out some code. **
Versions tested: Python 2.7.10 (OSX), Python 2.7.6 (Ubuntu 14.04).


No comments yet. Why don’t you start the discussion?

Leave a Reply