Search notes:

Python: context manager

A context manager is an object that is meant to manage allocation and freeing of resources. Such objects are able to be used in conjunction with the with statement.
A context manager needs to implement the __enter__ and __exit__ methods.

Typical example

A typical example for a context manager is using open() together with a with statement like so:
with open('foo.txt') as f:
     text = f.read()
When the with block is exited, the context manager automatically calls the f.close().

See also

contextmanager (in the standard library contextlib) is a decorator that turns a function into a context manager.

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758198544, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/objects/context-manager/index(49): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78