At 06:28 AM 3/16/2009 +1000, Nick Coghlan wrote:
There are some practical hurdles to that idea (specifically, creating a
callable which uses its parent's namespace rather than having its own),
but the basic concept seems sound.

Actually, that bit's pretty simple -- they're just "nonlocal" variables. But the cost of creating that function, and the cost of having cell variables in the surrounding function is potentially high. (In CPython at least, function calls are optimized in certain ways when the function only has "fast" locals, and no "cell" locals.)

The cost of creating the function (though not the code object) could be held till runtime, since the interpreter could wait until it's sure there's a __with__ method before doing a MAKE_CLOSURE on the code object.

Of course, at that point, what's the difference between:

    with foo() as bar:
        baz

and...

    @foo
    def bar():
       baz

except for being slightly less verbose? (due to missing nonlocal statements, etc.)

_______________________________________________
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

Reply via email to