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

2022年4月26日 11:20:07 -0700

On Apr 25, 2022, at 22:32, Larry Hastings <[email protected]> wrote:
> The general shape of it is the same. First, we have some sort of forward 
> declaration of the class. I'm going to spell it like this:
> 
> forward class C
> 
> just for clarity in the discussion. Note that this spelling is also viable:
> 
> class C
> 
> That is, a "class" statement without parentheses or a colon. (This is 
> analogous to how C++ does forward declarations of classes, and it was 
> survivable for them.) Another viable spelling:
> 
> C = ForwardClass()
I like this latter one exactly because as you say it doesn’t require any syntax 
changes. In the fine tradition of Python, we could certainly add syntactic 
sugar later, but I like that this can be implemented without it and we can see 
how the idea plays out in practice before committing to new syntax.
> This spelling is nice because it doesn't add new syntax. But maybe it's less 
> obvious what is going on from a user's perspective.
> 
> Whichever spelling we use here, the key idea is that C is bound to a 
> "ForwardClass" object. A "ForwardClass" object is not a class, it's a 
> forward declaration of a class. (I suspect ForwardClass is similar to a 
> typing.ForwardRef, though I've never worked with those so I couldn't say for 
> sure.)
I haven’t looked deeply at the code for ForwardRef, but I just cracked open 
typing.py and noticed this:
class ForwardRef(_Final, _root=True):
 """Internal wrapper to hold a forward reference."""
 __slots__ = ('__forward_arg__', '__forward_code__',
 '__forward_evaluated__', '__forward_value__',
 '__forward_is_argument__', '__forward_is_class__',
 '__forward_module__’)
So it seems that you’re almost there already!
> So, technically, this means we could spell the "continue class" step like so:
> 
> class C(BaseClass, ..., metaclass=MyMetaClass, __forward__=C):
> ...
Again, nice! Look Ma, no syntax changes.
I don’t know whether the __slots__ issue will break this idea but it is really 
... wonderful!
-Barry

Attachment: signature.asc
Description: Message signed with OpenPGP

_______________________________________________
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/KEZ3752H6DEZ25HJGMG3QZJYRCVJ5LCR/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to