Message118181
I find that I frequently need the "null" (no-op) context manager. For example, in code such as:
with transaction or contextlib.null():
...
Since there is no easy expression to create a null context manager, we must resort to workarounds, such as:
if transaction:
with transaction:
... code ...
else:
... duplicated code ...
(with the usual options of moving the duplicated code to a function—but still.)
Or by creating ad-hoc null context managers with the help of contextlib.contextmanager:
if transaction is None:
transaction = contextlib.contextmanager(lambda: iter([None])()
with transaction:
...
Adding a "null" context manager would be both practical and elegant. I have attached a patch for contextlib.py |
|
Date |
User |
Action |
Args |
2010-10-08 11:13:46 | hniksic | set | recipients:
+ hniksic |
2010-10-08 11:13:46 | hniksic | set | messageid: <1286536426.03.0.247784217987.issue10049@psf.upfronthosting.co.za> |
2010-10-08 11:13:44 | hniksic | link | issue10049 messages |
2010-10-08 11:13:43 | hniksic | create | |
|