is this a pattern I should be using?
In this particular case, I do think your pattern is not idiomatic, and potentially confusing to the reader of your code. The builtin print
(since this is a Python-3x question) already has a file
keyword argument which will do exactly what redirect_stdout
does in your example:
with open('myfile.txt', 'w') as myfile:
print('foo', file=myfile)
and introducing redirect_stdout
only makes your reader wonder why you don't use the builtin feature. (And personally, I find nested with
ugly. \
-separated with
even more ugly.)
As for the ease of commenting out (and for printing to both stdout
and a file), well you can have as many print
calls as you like, and comment them out as you need
with open('myfile.txt', 'w') as myfile:
print('foo')
print('foo', file=myfile)
Are there any other disadvantages I should know about
Nothing definite I can think of, except that it may not be the best solution (as in this case).
EDIT:
From the doc:
Note that the global side effect on sys.stdout means that this context
manager is not suitable for use in library code and most threaded
applications. It also has no effect on the output of subprocesses.