(original) (raw)

On Wed, Nov 8, 2017 at 5:49 PM, Nick Coghlan <ncoghlan@gmail.com> wrote:
On 8 November 2017 at 16:24, Guido van Rossum <guido@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 exposing
their code object the same way a function does (i.e. as a \`\_\_code\_\_\`
attribute), the introspection functions in \`dis\` would still work on
them, so you'd be able to look at things like which variable names
they referenced, thus granting the caller complete control over \*how\*
they resolved those variable names (by setting them in the local
namespace passed to the call).

I understood that they would be translated to \`lambda: \`. It seems you have a slightly more complex idea but if you're suggesting introspection through dis, that's too complicated for my taste.

This is why they'd have interesting potential future use cases as
general purpose callbacks - every local, nonlocal, global, and builtin
name reference would implicitly be an optional parameter (or a
required parameter if the name couldn't be resolved as a nonlocal,
global, or builtin).

Yeah, but that's scope creep for PEP 563\. Ɓukasz and I are interested in gradually restricting the use of annotations to static typing with an optional runtime component. We're not interested in adding different use cases. (We're committed to backwards compatibility, but only until 4.0, with a clear deprecation path.)
\> 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 I
didn't suggest (i.e. adding an opaque callable type that the dis and
inspect modules didn't understand). I agree that would be a bad idea,
but it's also not what I was suggesting we do.

I did not assume totally opaque -- but code objects are not very introspection friendly (and they have no strong compatibility guarantees).

Instead, thunks would offer all the same introspection features as
lambda 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 (the
same as a class namespace)

I don't understand the \_\_call\_\_ with "ns-None" thing but I don't expect it matters.
That said, even without a full-fledged thunk based solution to
handling lexical scoping I think there's a way to resolve the nested
class problem in PEP 563 that works for both explicitly and implicitly
quoted strings, while still leaving the door open to replacing
implicitly 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 into
the 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 f

That leaves the door open to a future PEP that proposes thunk-based
annotations as part of proposing thunks as a new low level delayed
evaluation primitive.

Sorry, that's not a door I'd like to leave open.

--
--Guido van Rossum (python.org/\~guido)