Python console rejects an object reference, having made an object with that reference as its name in previous line

Michael Torrie torriem at gmail.com
Mon Dec 15 00:27:05 EST 2014


On 12/14/2014 07:47 PM, Mark Lawrence wrote:
> I didn't realise that Python was so smart.  It can indicate a syntax 
> error at the final 't' in print before it gets to the opening bracket 
> that is required for the print function in Python 3 (and Python 2 if 
> you're using "from __future__ import print_function")?

Not really.  Python2 just interprets "print (value)" as an expression to
the print statement.  It's like saying "a=(value)" and then "print a".
Redundant but works.  However "print(value)" is interpreted as a
function call, and if you haven't imported it from future, it will error
out on Python2.

Python 3, on the other hand treats spaces between a function and its
opening paren to be optional whitespace.  Thus to python3,
"print(value)" and "print (value)" are the same.

So if you always put a space between print and (value) you can achieve
some measure of cross-version compatibility with print.  However by
including print_function from __future__ you are guaranteeing you won't
forget to add the parens.





More information about the Python-list mailing list