[Guido] > Going for all-out simplicity, I would like to be able to write these examples: > > class locking: > def __init__(self, lock): self.lock = lock > def __enter__(self): self.lock.acquire() > def __exit__(self, *args): self.lock.release() > > class opening: > def __init__(self, filename): self.filename = filename > def __enter__(self): self.f = open(self.filename); return self.f > def __exit__(self, *args): self.f.close()\ > > And do EXPR as VAR: BLOCK would mentally be translated into > > itr = EXPR > VAR = itr.__enter__() > try: BLOCK > finally: itr.__exit__(*sys.exc_info()) # Except sys.exc_info() isn't > defined by finally
Yeah, that's what I wanted to do too. That should be about what my second suggestion did. Slightly updated, it looks like: stmt = EXPR1 VAR1 = stmt.__enter__() exc = () # or (None, None, None) if you prefer try: try: BLOCK1 except: exc = sys.exc_info() finally: if stmt.__exit__(*exc) is not None: raise exc[0], exc[1], exc[2] The only difference should be that with the above semantics if you return a (non-None) value from __exit__, the exception will be suppressed (that is, it will not be reraised). Means that if you want to suppress an exception, you have to add a return statement (but if you want exceptions to be reraised, you don't have to do anything.) I suggest this only because there were a few suggested use-cases for suppressing exceptions. OTOH, almost all my uses are just try/finally's so I'm certainly not going to cry if that last finally instead looks like: finally: stmt.__exit__(*exc) raise exc[0], exc[1], exc[2] =) STeVe -- You can wordify anything if you just verb it. --- Bucky Katt, Get Fuzzy _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com