June 2017
Beginner
352 pages
8h 39m
English

The with-block syntax looks like this:
with EXPR as VAR: BLOCK
This is so-called syntactic sugar for a much more complex arrangement of try...except and try...finally blocks:
mgr = (EXPR)exit = type(mgr).__exit__ # Not calling it yetvalue = type(mgr).__enter__(mgr)exc = Truetry: try: VAR = value # Only if "as VAR" is present BLOCK except: # The exceptional case is handled here exc = False if not exit(mgr, *sys.exc_info()): raise # The exception is swallowed if exit() returns truefinally: # The normal and non-local-goto cases are handled here if exc: exit(mgr, None, None, None)
Which ...
Read now
Unlock full access