This is really a few questions in one.
First, the "how" question:
The >>
token is not actually an operator here; it's part of the syntax of the print
statement, as documented here. The grammar is:
print_stmt ::= "print" ([expression ("," expression)* [","]]
| ">>" expression [("," expression)+ [","]])
… and the semantics are:
This form is sometimes referred to as “print chevron.” In this form, the first expression after the
>>
must evaluate to a “file-like” object, specifically an object that has awrite()
method as described above. With this extended form, the subsequent expressions are printed to this file object.
Second, the "why" question:
Early on, the Python developers thought this was a convenient way to write to files.
Since then, they've added stdout redirection, and more powerful file and string-formatting APIs, so it's not as useful anymore. And, thanks to other modern features like keyword arguments, and a few decades to think about it, they've come up with a more flexible way to design print
as a regular function that doesn't require any special syntax at all. But removing it would break backward compatibility, so it couldn't be removed until 3.0.
And it was removed in 3.0; you're only still seeing it because you're using an older version.
If you want the new print
function in 2.7, however, you can use a future statement: from __future__ import print_function
. But that will of course break print >>foo, spam
; you'll have to rewrite it as print(spam, file=foo)
.