r/Python • • Feb 28 '13

What's the one code snippet/python trick/etc did you wish you knew when you learned python?

I think this is cool:

import this

259 Upvotes

307 comments sorted by

View all comments

Show parent comments

3

u/jabbalaci Feb 28 '13
import sys

print >>sys.stderr, "Warning! Reactor breach. Leave the facility immediately!"

with open("out.txt", "w") as f:
    print >>f, "first line"

0

u/[deleted] Feb 28 '13

[deleted]

4

u/earthboundkid Feb 28 '13

The double bracket syntax is an ugly hack. They fixed it in Python 3 by making print a function. Just use Python 3 and try to forget that awful double bracket thing was ever necessary. ;-)

2

u/ColOfNature Feb 28 '13

Or

from __future__ import print_function

If you can't make the switch to 3 but still want that print() goodness.

E: just noticed someone below already suggested this. Ho-hum.

1

u/seriouslulz Mar 04 '13

Yeah, just use sys.stderr.write('foo\n')