C. Greve
C. Greve

Reputation: 1

Strange parentheses

I was just using pydev for eclipse running python2.7 for an Intro to Programming course (which requires python3.x) and so I was using the updated print statement with parentheses

The issue that I am running into is that after my final while loop, the print() statement includes the parentheses.

print('Number of flushes: ', flush)

returns

('Number of flushes: ', 0)

The weird thing is that earlier in the program, before the while loop ended, the print() statement worked exactly as expected. What is going on?

Upvotes: 0

Views: 77

Answers (2)

beigel
beigel

Reputation: 1200

In Python 2.X, print is a statement, not s function. Putting parenthesis around the arguments is fine when there's only one, but when there are two or more the arguments are treated as a tuple. One way around this is to insert at the top of your code the statement, from __future__ import print_function. This turns the print statement into a function and gives the expected behavior when printing two or more items. However, then you must turn all the print statements into the function form.

Upvotes: 1

Yu Hao
Yu Hao

Reputation: 122383

In Python 2.x, print is a statement, not a function, so here:

print('Number of flushes: ', flush)

is valid syntax, but the parenthesis is interpreted as a tuple:

>>> print 3, 4
3 4
>>> print (3, 4)
(3, 4)

Upvotes: 4

Related Questions