New submission from Hrvoje Nikšić <hnik...@gmail.com>:

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

----------
files: fl
messages: 118181
nosy: hniksic
priority: normal
severity: normal
status: open
title: Add the null context manager to contextlib
Added file: http://bugs.python.org/file19158/fl

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue10049>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to