with is often used to open files, using with can automatically close even if an error occurs
What is context?
The context is actually blunt, and the context of the article is a meaning, in a bit more popular, I think it is better to call the environment
Although the context is called a context, it is usually only the above in the program, but it is just a nice call to the context. .
There are ups and downs in process interruption in the operating system, but do n’t delve into this deep question.
Any object that implements the enter () and exit () methods can be called a context manager. The context manager object can use the with keyword. Obviously, the file object also implements the context manager.
So how do file objects implement these two methods? We can simulate and implement an own file class and let the class implement the enter () and exit () methods.