3 ms·
Python has this with the "with" statement. It's often used for files, but you can write your own with classes and an __enter__ and __exit__ method. However you
by rmc 15y ago
Python has this with the "with" statement. It's often used for files, but you can write your own with classes and an __enter__ and __exit__ method.
However you can use the ``contextlib`` module to make really simple ``contextmanger``'s with the yield statement
e.g.:
from contextlib import contextmanager
@contextmanager
def glcontext():
glBegin()
yield
glEnd()
(This is an example where someone wanted to do OpenGL in python)