[Python-Dev] Re: Proto-PEP part 4: The wonderful third option

2022年4月26日 00:03:11 -0700

> On 26 Apr 2022, at 07:32, Larry Hastings <[email protected]> wrote:
> 
> 
[... snip ...]
> Next we have the "continue" class statement. I'm going to spell it like this:
> 
> continue class C(BaseClass, ..., metaclass=MyMetaclass):
> # class body goes here
> ...
> 
> I'll mention other possible spellings later. The first change I'll point out 
> here: we've moved the base classes and the metaclass from the "forward" 
> statement to the "continue" statement. Technically we could put them either 
> place if we really cared to. But moving them here seems better, for reasons 
> you'll see in a minute.
> 
> Other than that, this "continue class" statement is similar to what I (we) 
> proposed before. For example, here C is an expression, not a name.
> 
> Now comes the one thing that we might call a "trick". The trick: when we 
> allocate the ForwardClass instance C, we make it as big as a class object can 
> ever get. (Mark Shannon assures me this is simply "heap type", and he knows 
> far more about CPython internals than I ever will.) Then, when we get to the 
> "continue class" statement, we convince metaclass.__new__ call to reuse this 
> memory, and preserve the reference count, but to change the type of the 
> object to "type" (or what-have-you). C has now been changed from a 
> "ForwardClass" object into a real type. (Which almost certainly means C is 
> now mutable.)
> 
A problem with this trick is that you don’t know how large a class object can 
get because a subclass of type might add new slots. This is currently not 
possible to do in Python code (non-empty ``__slots__`` in a type subclass is 
rejected at runtime), but you can do this in C code.
Ronald
—
Twitter / micro.blog: @ronaldoussoren
Blog: https://blog.ronaldoussoren.net/
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/JMO4V4S6OHOFFSKQ3XGU2AEEQVYUAY6J/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to