Created
January 10, 2019 12:39
-
-
Save wwj718/90cbcba431daae59adf36f399b35587e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from io import StringIO | |
import contextlib | |
@contextlib.contextmanager | |
def stdoutIO(stdout=None): | |
old = sys.stdout | |
if stdout is None: | |
stdout = StringIO() | |
sys.stdout = stdout | |
yield stdout | |
sys.stdout = old | |
code = """ | |
i = [0,1,2] | |
for j in i : | |
print(j) | |
""" | |
with stdoutIO() as s: | |
exec(code) | |
print("out:", s.getvalue()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment