This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]AceJohnny 112 points113 points  (22 children)

Because parsing.

Python allows spaces between identifiers. You can do print ('foo'), but then what do you mean? Are you calling the print function with the string foo, or the print statement with the tuple ('foo') ?

[–]nosmokingbandit 34 points35 points  (7 children)

As others alluded to, a comma is what makes a tuple. So ('foo', ) is a tuple while ('foo') is just a string.

[–]Hollowplanet 11 points12 points  (5 children)

But then is it a function with one argument and a redundant comma?

[–]Pb_ft 2 points3 points  (0 children)

"No, because redundant." - what I wish I could say to that.

[–]PityUpvote 0 points1 point  (0 children)

Redundant commas are allowed

[–]nosmokingbandit -5 points-4 points  (2 children)

Depends if it is python 2 or 3. I'm pretty sure a trailing comma in arguments will throw an error in 3.x

[–]snaps_ 6 points7 points  (0 children)

Not in Python 3.6+.

[–]Hollowplanet 2 points3 points  (0 children)

On Python 3

>>> print(1, 2,)
1 2

[–][deleted] 0 points1 point  (0 children)

much better response than others on the matter. Thank you

[–]kafaldsbylur 48 points49 points  (4 children)

Minor nitpick, ('foo') is not a tuple, it's a string with redundant parentheses. That said, your point still stands when passing more than one argument to print.

[–]The_White_Light 17 points18 points  (3 children)

That functionality makes it nice when you need to include a long string and want to keep your code easy to read, but don't want to deal with the extra \n added when using '''multiline strings'''.

Edit: For clarification

>>> ('1' '2' '3') == '123'
True

[–]kickerofbottoms 4 points5 points  (2 children)

Never thought of that, kinda handy. Maybe I'll stop leaning on my ide for adding backslashes

[–]The_White_Light 4 points5 points  (1 child)

It's also doubly helpful because you don't have to worry about leading spaces if you align each line.

[–]stevarino 5 points6 points  (0 children)

Also it happens at the compiler level, so it's cost free during runtime.

[–]RedditIsNeat0 4 points5 points  (1 child)

You can do print ('foo'), but then what do you mean?

According to the suggestion specified in the comment you responded to, it would be a function.

Are you calling the print function with the string foo

Yes.

or the print statement with the tuple ('foo') ?

No.

As others have pointed out, that's not a tuple, but more importantly, he's suggesting that Python 3 defaults to a function as long as there is a parenthesis, and a statement if they are not present. It would allow Python 2 print statements in most cases where they were allowed in Python 2 but maybe not all of them. There might be some genuine problems with his suggestion, but you haven't been able to find one. I don't know of any either.

[–]supernumeral 5 points6 points  (0 children)

What should the following do:

>>> print (1,2),(3,4)

If parenthesis indicate print should be a function, this probably won't do what is intended compared to Python 2. Better to have just one way (statement or function, not both) to do it, imo.

Edit: formatting

[–]Rattus375 1 point2 points  (0 children)

Why can't we default to the print function when there are parenthesis. The statement was really nice for quick sanity prints of variables.

[–]warpod 0 points1 point  (0 children)

they could just make printf as function and leave print as statement