Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/user/bin/python3
- from typing import TextIO
- from sys import stdout, stderr
- _print_context: TextIO = stdout
- def print(*values, sep: str = ' ', end: str = '\n', file: TextIO = None, flush: bool = False) -> int:
- out = _print_context if file is None else file
- c_length = out.write(sep.join(map(str, values)) + end)
- if flush:
- out.flush()
- return c_length
- class p_switcher:
- _last_stream: TextIO
- _this_stream: TextIO
- def __init__(self, out: TextIO):
- self._last_stream = _print_context
- self._this_stream = out
- def __enter__(self):
- global _print_context
- _print_context = self._this_stream
- def __exit__(self, exc_type, exc_val, exc_tb):
- global _print_context
- _print_context = self._last_stream
- if __name__ == '__main__':
- print('Hello World!')
- with p_switcher(stderr):
- print('Hi', 5, flush = True)
- print('This is', stdout, 'safe and sound.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement