[Tutor] print statement vs print() function, was Re: Increase performance of the script
Peter Otten
__peter__ at web.de
Tue Dec 11 13:42:40 EST 2018
Asad wrote:
> Hi All,
>
> I used your solution , however found a strange issue with deque
> :
>
> I am using python 2.6.6:
Consider switching to Python 3; my code as posted already works with that.
>>>> import collections
>>>> d = collections.deque('abcdefg')
>>>> print 'Deque:', d
> File "<stdin>", line 1
> print 'Deque:', d
> ^
> SyntaxError: invalid syntax
This is the effect of a
from __future__ import print_function
import. Once that is used print becomes a function and has to be invoked as
such.
If you don't want this remove the "from __future__ print_function" statement
and remove the parentheses from all print-s (or at least those with multiple
arguments).
Note that in default Python 2
print("foo", "bar")
works both with and without and parens, but the parens constitute a tuple.
So
>>> print "foo", "bar" # print statement with two args
foo bar
>>> print ("foo", "bar") # print statement with a single tuple arg
('foo', 'bar')
>>> from __future__ import print_function
>>> print "foo", "bar" # illegal, print() is now a function
File "<stdin>", line 1
print "foo", "bar"
^
SyntaxError: invalid syntax
>>> print("foo", "bar") # invoke print() function with two args
foo bar
More information about the Tutor
mailing list