Movatterモバイル変換


[0]ホーム

URL:


[Python-Dev] PEP 563: Postponed Evaluation of Annotations

Nick Coghlanncoghlan at gmail.com
Wed Nov 8 20:49:01 EST 2017


On 8 November 2017 at 16:24, Guido van Rossum <guido at python.org> wrote:> I also don't like the idea that there's nothing you can do with a thunk> besides calling it -- you can't meaningfully introspect it (not without> building your own bytecode interpreter anyway).Wait, that wasn't what I was suggesting at all - with thunks exposingtheir code object the same way a function does (i.e. as a `__code__`attribute), the introspection functions in `dis` would still work onthem, so you'd be able to look at things like which variable namesthey referenced, thus granting the caller complete control over *how*they resolved those variable names (by setting them in the localnamespace passed to the call).This is why they'd have interesting potential future use cases asgeneral purpose callbacks - every local, nonlocal, global, and builtinname reference would implicitly be an optional parameter (or arequired parameter if the name couldn't be resolved as a nonlocal,global, or builtin).> Using an AST instead of a string is also undesirable -- the AST changes in> each release, and the usual strong compatibility guarantees don't apply> here. And how are you going to do anything with it? If you've got a string> and you want an AST node, it's one call away. But if you've got an AST node> and you want either a string *or* the object to which that string would> evaluate, you've got a lot of work to do. Plus the AST takes up a lot more> space than the string, and we don't have a way to put an AST in a bytecode> file. (And as Inada-san pointed out a thunk *also* takes up more space than> a string.)>> Nick, please don't try to save the thunk proposal by carefully dissecting> every one of my objections. That will just prolong its demise.Just the one objection, since you seem to be rejecting something Ididn't suggest (i.e. adding an opaque callable type that the dis andinspect modules didn't understand). I agree that would be a bad idea,but it's also not what I was suggesting we do.Instead, thunks would offer all the same introspection features aslambda expressions do, they'd just differ in the following ways:* the parameter list on their code objects would always be empty* the parameter list for their __call__ method would always be "ns=None"* they'd be compiled without CO_OPTIMIZED (the same as a class namespace)* they'd look up their closure references using LOAD_CLASSDEREF (thesame as a class namespace)That said, even without a full-fledged thunk based solution tohandling lexical scoping I think there's a way to resolve the nestedclass problem in PEP 563 that works for both explicitly and implicitlyquoted strings, while still leaving the door open to replacingimplicitly quoted strings with thunks at a later date: stating that*if* users want such nested references to be resolvable at runtime,they need to inject a runtime reference to the outermost class intothe inner class namespace.That is, if you want to take:    class C:        field = 1        class D:            def method(a: C.field):                ...and move it inside a function, that would actually look like:    def f():        class C:            field = 1            class D:                def method(a: C.field):                    ...        C.D.C = C # Make annotations work at runtime        return fThat leaves the door open to a future PEP that proposes thunk-basedannotations as part of proposing thunks as a new low level delayedevaluation primitive.Cheers,Nick.-- Nick Coghlan   |ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Devmailing list

[8]ページ先頭

©2009-2025 Movatter.jp