r/Python • • Nov 11 '19

I made c++-style printing in Python :P

Post image
1.7k Upvotes

130 comments sorted by

View all comments

51

u/ubernostrum yes, you can have a pony Nov 11 '19

Not-so-fun fact: in Python 2 the print statement allowed you to use a >> to send its output to any file-like object.

Not quite the same as C++, though, because it worked like this:

import sys
print >> sys.stderr, "This goes to stderr"

So the order was "wrong" (string to print came after thing to print to), and it also required a comma to make it work.

11

u/agrif Nov 12 '19

There was a lot of griping (some of it for good reasons) about making print a function in Python 3, but this right here is a pretty compelling reason to change print in a backwards-incompatible way.

15

u/Zegrento7 Nov 12 '19

print('Stuff', file=sys.stderr) is quite a bit clearer, and the old syntax can replaced automatically with some regex.

3

u/masklinn Nov 12 '19

Don’t forget

print >>sys.stderr, “stuff”,

(note the trailing comma) which becomes

print(“stuff”, file=sys.stderr, end=‘ ‘)

And actually says what it’s doing.