What is Python the operator is used with?

I am trying to understand the with statement in python. Everywhere I watch him talk about opening and closing a file and is intended to replace the try-finally block. Can anyone post some other examples. I just tried the flask and it has a lot of applications. Definitely ask someone to give me some clarity.

+4
source share
3 answers

Here is a very good explanation here . Basically, the with statement invokes two special methods for the associated object. The __enter__ and __exit__ methods. The enter method returns the variable associated with the with statement. Although the __exit__ method is called after the operator processes any cleanup (for example, closing the file pointer).

+9
source

The idea with is to make the “right thing” the least resistance. While the file example is the simplest, thread locks actually provide a more classic code example with a clear error:

 try: lock.acquire() # do stuff finally: lock.release() 

This code does not work - if the lock collection does not work, either an incorrect exception will occur (since the code will try to release the lock that it never received), or, even worse, if it is a recursive lock, it will be released early. The correct code is as follows:

 lock.acquire() try: # do stuff finally: # If lock.acquire() fails, this *doesn't* run lock.release() 

Using the with statement, it becomes impossible to get it wrong, because it is built into the context manager:

 with lock: # The lock *knows* how to correctly handle acquisition and release # do stuff 

Another place where the with statement helps a lot is similar to the main advantage of function and class decorators: it requires two-part code, which can be divided by an arbitrary number of lines of code (definition of a function for decorators, try block in the current case) and turns it into code "one part" where the programmer simply states what he is trying to do.

For brief examples, this doesn't seem like a big win, but it really matters a lot when viewing the code. When I see lock.acquire() in a piece of code, I need to scroll down and check the corresponding lock.release() . However, when I see with lock: such a check is not required - I immediately see that the lock will be released correctly.

+9
source

There are 12 examples of using with in PEP343 , including an example with an open file:

  • A pattern for securing a lock acquired at the beginning of a block is issued when the block remains
  • A template for opening a file, which ensures that the file is closed when the block remains
  • Template for fixing or returning a deal database
  • Example 1 rewritten without generator
  • Temporary redirect stdout
  • The open () option, which also returns an error condition
  • Another useful example would be an operation that blocks Signals.
  • Another use case for this function is the Decimal context.
  • Here is a simple context manager for the decimal module
  • General object-closure context manager
  • in the released () context to temporarily release a previously acquired lock by replacing the receive () and release () calls
  • a "nested" context manager that automatically provides contexts from left to right to avoid excessive indentation
+1
source

All Articles