copy.copy()

copy.copy(x) Return a shallow copy of x.

contextlib.suppress()

contextlib.suppress(*exceptions) Return a context manager that suppresses any of the specified exceptions if they occur in the body of a with statement and then resumes execution with the first statement following the end of the with statement. As with any other mechanism that completely suppresses exceptions, this context manager should be used only to cover very specific errors where silently continuing with program execution is known to be the right thing to do. For example: from contextl

contextlib.ExitStack.enter_context()

enter_context(cm) Enters a new context manager and adds its __exit__() method to the callback stack. The return value is the result of the context manager’s own __enter__() method. These context managers may suppress exceptions just as they normally would if used directly as part of a with statement.

contextlib.redirect_stderr()

contextlib.redirect_stderr(new_target) Similar to redirect_stdout() but redirecting sys.stderr to another file or file-like object. This context manager is reentrant. New in version 3.5.

contextlib.redirect_stdout()

contextlib.redirect_stdout(new_target) Context manager for temporarily redirecting sys.stdout to another file or file-like object. This tool adds flexibility to existing functions or classes whose output is hardwired to stdout. For example, the output of help() normally is sent to sys.stdout. You can capture that output in a string by redirecting the output to an io.StringIO object: f = io.StringIO() with redirect_stdout(f): help(pow) s = f.getvalue() To send the output of help() to a f

contextlib.ExitStack.push()

push(exit) Adds a context manager’s __exit__() method to the callback stack. As __enter__ is not invoked, this method can be used to cover part of an __enter__() implementation with a context manager’s own __exit__() method. If passed an object that is not a context manager, this method assumes it is a callback with the same signature as a context manager’s __exit__() method and adds it directly to the callback stack. By returning true values, these callbacks can suppress exceptions the same

contextlib.ExitStack.pop_all()

pop_all() Transfers the callback stack to a fresh ExitStack instance and returns it. No callbacks are invoked by this operation - instead, they will now be invoked when the new stack is closed (either explicitly or implicitly at the end of a with statement). For example, a group of files can be opened as an “all or nothing” operation as follows: with ExitStack() as stack: files = [stack.enter_context(open(fname)) for fname in filenames] # Hold onto the close method, but don't call it

contextlib.ExitStack.close()

close() Immediately unwinds the callback stack, invoking callbacks in the reverse order of registration. For any context managers and exit callbacks registered, the arguments passed in will indicate that no exception occurred.

contextlib.ContextDecorator

class contextlib.ContextDecorator A base class that enables a context manager to also be used as a decorator. Context managers inheriting from ContextDecorator have to implement __enter__ and __exit__ as normal. __exit__ retains its optional exception handling even when used as a decorator. ContextDecorator is used by contextmanager(), so you get this functionality automatically. Example of ContextDecorator: from contextlib import ContextDecorator class mycontext(ContextDecorator): def

contextlib.closing()

contextlib.closing(thing) Return a context manager that closes thing upon completion of the block. This is basically equivalent to: from contextlib import contextmanager @contextmanager def closing(thing): try: yield thing finally: thing.close() And lets you write code like this: from contextlib import closing from urllib.request import urlopen with closing(urlopen('http://www.python.org')) as page: for line in page: print(line) without needing to expl