Fluent Python: Clear, Concise, and Effective Programming
Rate it:
Open Preview
64%
Flag icon
The with statement was designed to simplify some common uses of try/finally, which guarantees that some operation is performed after a block of code, even if the block is terminated by return, an exception, or a sys.exit() call. The code in the finally clause usually releases a critical resource or restores some previous state that was temporarily changed.
64%
Flag icon
The context manager interface consists of the __enter__ and __exit__ methods. At the top of the with, Python calls the __enter__ method of the context manager object. When the with block completes or terminates for any reason, Python calls __exit__ on the context manager object.
65%
Flag icon
the context manager object is the result of evaluating the expression after with, but the value bound to the target variable (in the as clause) is the result returned by the __enter__ method of the context manager object.
65%
Flag icon
When real applications take over standard output, they often want to replace sys.stdout with another file-like object for a while, then switch back to the original. The contextlib.redirect_stdout context manager does exactly that: just pass it the file-like object that will stand in for sys.stdout.
65%
Flag icon
Using @contextmanager reduces the boilerplate of creating a context manager: instead of writing a whole class with __enter__/__exit__ methods, you just implement a generator with a single yield that should produce whatever you want the __enter__ method to return.
65%
Flag icon
In a generator decorated with @contextmanager, yield splits the body of the function in two parts: everything before the yield will be executed at the beginning of the with block when the interpreter calls __enter__; the code after yield will run when __exit__ is called at the end of the block.
65%
Flag icon
Having a try/finally (or a with block) around the yield is an unavoidable price of using @contextmanager, because you never know what the users of your context manager are going to do inside the with block.
66%
Flag icon
Extensive use of recursion and minimal use of assignment are hallmarks of programming in a functional style.
66%
Flag icon
Using the terminology of the Python interpreter, the output of parse is an AST (Abstract Syntax Tree): a convenient representation of the Scheme program as nested lists forming a tree-like structure, where the outermost list is the trunk, inner lists are the branches, and atoms are the leaves
1 10 12 Next »