onelinerizer
onelinerizer copied to clipboard
Why not support "with" keyword?
Does not below two ways equal?
class Foo(object):
def __enter__(self):
print "hello, world"
def __exit__(self, type, value, tb):
print "bye"
with Foo() as foo:
pass
mgr = Foo()
exit = type(mgr).__exit__
value = type(mgr).__enter__(mgr)
exc = True
try:
try:
foo = value
pass
except:
exc = False
if not exit(mgr, *sys.exc_info()):
raise
finally:
if exc:
exit(mgr, None, None, None)
We’re familiar with PEP 343, yes. This is an implementation challenge, not a conceptual challenge. (Learning how to catch exceptions was the conceptual challenge.) There’s a not-quite-working attempt to support with at #59.
@andersk Thanks, but yield has some theoretical limits to be supported, doesn't it? Lambda calculus can not express call/cc simply.