I was looking through some Python source code, when I came across this:
print >> sys.stderr, __doc__
What does the >> mean? I've never seen syntax like this before.
I was looking through some Python source code, when I came across this:
print >> sys.stderr, __doc__
What does the >> mean? I've never seen syntax like this before.
See the "print chevron" description in the Python 2.7 docs:
>>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. If the first expression evaluates toNone, thensys.stdoutis used as the file for output.
The special syntax has disappeared in Python 3, as print was converted from a statement to a function.
This syntax is specific to the print statement. Rather than the output going to standard output it sends the output to the file-like named after the >>, in this case standard error.
In an expression this is the right shift operator.