On Tue, Apr 26, 2022 at 7:24 PM Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
On 27/04/22 2:01 am, Chris Angelico wrote:
> That would be the case if monkeypatching were illegal. Since it's not,
> wherein lies the difference?

The proposed feature is analogous to forward declaring a
struct in C. Would you call what C does monkeypatching?

It is not analogous; it is a false analogy that obscures the issues with this proposal in Python.

A C forward declaration (not to mention the full struct declaration also!) is purely for the compiler; at runtime one can have a pointer to some memory that the compiler expects to be shaped like that struct, but one can never get hold of any runtime value that is “the struct definition itself,” let alone a runtime value that is the nascent forward-declared yet-to-be-completed struct. So clearly there can be no patching of something that never exists at runtime at all. 

Python is quite different from C in this respect.  Classes are runtime objects, and so is the “forward declared class” object. The proposal is for a class object to initially at runtime be the latter, and then later (at some point that is not well defined if the implementation is in a separate module, because global module import ordering is an unstable emergent property of all the imports in the entire codebase) may suddenly, everywhere and all at once, turn into the former. Any given module that imports the forward declared name can have no guarantee when (if ever) that object will magically transform into something that is safe to use. 

Whether we call it monkeypatching or not is irrelevant. Having global singleton objects change from one thing to a very different thing, at an unpredictable point in time, as a side effect of someone somewhere importing some other module, causes very specific problems in being able to locally reason about code. I think it is more useful to discuss the specific behavior and its consequences than what it is called. 

Carl