3

Looking at the source code of pstats I see this syntax:

print >> self.stream, "in %.3f seconds" % self.total_tt
print >> self.stream

What is this syntax, how is it called and how to use it? I have never seen it before, nor seen it in any of the Python books/tutorials I have read.

orlp
  • 112,504
  • 36
  • 218
  • 315

2 Answers2

6

If you mean the >>, that's the Python 2.x syntax for writing to a file-like other than sys.stdout with print. It's defined in the Python docs, 6.6. The print statement and has been around since at least Python 2.5 (and I think earlier).

This syntax has been replaced with a file kwarg to the print function in Python 3.0.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • 1
    It might be also helpful to think of it as python's version of fprintf, but instead of c's file handles, you specify a python "file-like object". – jedwards Nov 07 '11 at 10:48
2

It's the extended form of the print statement which redirects the output to the file-like object immediately following it. See the Python docs.

martineau
  • 119,623
  • 25
  • 170
  • 301