[pyar] Puedo evitar ejecución del bloque con un Context Manager?

Claudio Freire klaussfreire en gmail.com
Lun Jun 17 19:27:54 ART 2013


2013/6/15 Lucio Torre <lucio.torre en gmail.com>:
> Esto a lo mejor tendria mas sentido asi:
>
> if not task_done(task_name):
>      with task_monitor(task_name):
>           do_task()


O, al revés:

>>> class task_monitor(object):
...     def __init__(self):
...         self.done = False
...     def __enter__(self):
...         return self.done
...     def __exit__(self, type, value, traceback):
...         if type is None: # <- se hizo sólo si no tiró excepción
...             self.done = True
...
>>> this_task_monitor = task_monitor()
>>> that_task_monitor = task_monitor()
>>> with this_task_monitor as was_done:
...     if not was_done:
...          print "this"
...
this
>>> with that_task_monitor as was_done:
...     if not was_done:
...          print "that"
...
that
>>> with this_task_monitor as was_done:
...     if not was_done:
...          print "this"
...
>>> with that_task_monitor as was_done:
...     if not was_done:
...          print "that"
...
>>>


More information about the pyar mailing list