onelinerizer icon indicating copy to clipboard operation
onelinerizer copied to clipboard

Why not support "with" keyword?

Open ethe opened this issue 9 years ago • 2 comments

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)

ethe avatar May 31 '16 17:05 ethe

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 avatar May 31 '16 18:05 andersk

@andersk Thanks, but yield has some theoretical limits to be supported, doesn't it? Lambda calculus can not express call/cc simply.

ethe avatar Jun 01 '16 05:06 ethe