[Tutor] Problem with print

Hugo Arts hugo.yoshi at gmail.com
Tue Dec 21 08:24:20 CET 2010

On Tue, Dec 21, 2010 at 6:08 AM, David Hutto <smokefloat at gmail.com> wrote:
>> Note that print can actually only be used as a statement in python 2.
>> It just so happens that you can include parentheses in the statement.
>> Though that makes it look similar to a function call, it certainly is
>> not. You'll see that when you try to supply multiple arguments to the
>> "function." This makes python suddenly interpret the parentheses as a
>> tuple, making it obviously not a function call.
> However, it's declared as a NoneType in Python 3.1.2:
>>>> x = print('test','test1')
> test test1
>>>> x
>>>> type(x)
> <class 'NoneType'>
> I haven't gone further than that to test it's 'tupleness', or that
> might not have been what you meant by it being 'interpreted as a
> tuple'.

Sorry, I haven't made myself clear. In python3, print is a function,
and it return None. I'm not talking about python3 now, though.
Everything below will refer to python 2.x

In python2, print is a statement. The print statement in python2 can
sometimes look like a function call, but it never is:

Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello")

See? it looks like a function call, but it actually isn't. for proof,
try to assign its return value:

>>> a = print("hello")
  File "<stdin>", line 1
    a = print("hello")
SyntaxError: invalid syntax

The parentheses that make this thing look like a function call so much
are just regular old parentheses. This becomes apparent when we
compare the output from these two statements:

>>> print('a', 'b')
('a', 'b')
>>> print 'a', 'b'
a b

The parentheses are part of what's being printed now! That's because
('a', 'b') is interpreted as a tuple in this case. In python3, it
would have been interpreted as a call to the print function, with
arguments 'a' and 'b'. But in python2 you can't even have a function
named print, because print is a keyword used in the print statement.


More information about the Tutor mailing list