parent
45c25ac443
commit
944216964a
16
pep-0340.txt
16
pep-0340.txt
|
@ -579,6 +579,22 @@ Examples
|
|||
self.lock.release()
|
||||
raise type, value, traceback
|
||||
|
||||
7. Redirect stdout temporarily:
|
||||
|
||||
def saving_stdout(new_stdout):
|
||||
save_stdout = sys.stdout
|
||||
try:
|
||||
sys.stdout = new_stdout
|
||||
yield
|
||||
finally:
|
||||
sys.stdout = save_stdout
|
||||
|
||||
Used as follows:
|
||||
|
||||
block opening(filename, "w") as f:
|
||||
block saving_stdout(f):
|
||||
print "Hello world"
|
||||
|
||||
Acknowledgements
|
||||
|
||||
In no useful order: Alex Martelli, Barry Warsaw, Bob Ippolito,
|
||||
|
|
Loading…
Reference in New Issue