x = eval(repr(sys.stdout)) ?
Paul Rubin
phr-n2002b at NOSPAMnightsong.com
Wed Aug 7 06:57:40 EDT 2002
Robert McLay <mclay at cfdlab.ae.utexas.edu> writes:
> So the question is: Is there a way to save a "pointer" to a variable in
> python so that it can be set latter?
> ...
> and foo is now 2. Is there something that can do this in python?
No not really in full generality, unless you count exec/eval.
For what you describe, you could use a closure:
def temp_redirect(new_stream):
old_stream = sys.stdout
sys.stdout = new_stream
def _restore():
sys.stdout = old_stream
return _restore
and then to do your redirection:
restore = temp_redirect(open("whatever","w")) # redirects stdout
...
and then when you're done:
restore()
resets sys.stdout to its old value.
More information about the Python-list
mailing list