Python context Manager

Source: Internet
Author: User

The context Manager enables allocation and deallocation of resources by using more readable and streamlined code

The most common use of the context manager is with the WITH statement, which constructs the allocation of resources and the release of syntactic sugars. Take the most common example, the opening and closing of a file.

Normal syntax:

f = file (filename,'w')   try:      f.write ('  Test file')   finally:      f.close ()

That is, normally, you want to display the open and close files. But if you use the WITH statement, it will be more readable and never worry about forgetting to close the file:

With file (filename,'w') as  F:      f.write ('test file ')

Use in TensorFlow:

Python context Manager

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.