[Python-ideas] Fast context creation

Juancarlo Añez apalala at gmail.com
Thu Nov 27 14:18:39 CET 2014


On Thu, Nov 27, 2014 at 7:39 AM, Stefano Borini <
stefano.borini at ferrara.linux.it> wrote:

>
>     with contextlib.context(enter=None, exit=lambda *args:
> my_widget.setFocus()):
>         do what I need to do
>

You can easily define your own context manager to do what you want:

    @contextmanager
    def finally_focus(widget):
        try:
            yield
        finally:
            widget.setFocus()

    with finally_focus(my_widget):
        # do what I need to do


-- 
Juancarlo *Añez*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20141127/1799b9f3/attachment.html>


More information about the Python-ideas mailing list