On 4/23/22 08:57, Eric V. Smith wrote:
On 4/23/2022 9:55 AM, Jelle Zijlstra wrote:

However, it doesn't solve the problem for base classes. For example, str is conceptually defined as `class str(Sequence["str"]):`. A forward reference can't make `str` defined when the bases are evaluated, because bases are resolved at the `forward class` stage.

Larry's second email "Proto-PEP part 2: Alternate implementation proposal for "forward class" using a proxy object" discusses a possibility to move the bases and metaclasses to the "continue class" stage. It also has the advantage of not changing the behavior of __new__, and I think is in general easier to reason about.


Let me expound on Eric's statement for a bit.  Moving the base classes and metaclass to the "continue" statement might permit the self-referential "str" definition suggested above:

   forward class str

   ...

   continue class str(Sequence[str]):
        ...

Though I suspect this isn't viable, or at least not today.  I'm willing to bet a self-referential definition like this would result in an infinite loop when calculating the MRO.


I don't have a strong sense of whether it'd be better to have the base classes and metaclass defined with the "forward" declaration or the "continue" declaration.  The analogous syntax in C++ has the base classes defined with the "continue" class.

Actually, that reminds me of something I should have mentioned in the proto-PEP.  If the "class proxy" version is viable and desirable, and we considered moving the base classes and metaclass down to the "continue" statement, that theoretically means we could drop the "forward" and "continue" keywords entirely.  I prefer them, simply because it makes it so explicit what you're reading.  But the equivalent syntax in C++ doesn't bother with extra keywords for either the "forward" declaration or the "continue" declaration, and people seem to like it fine.  Using that variant of the syntax, the toy example from the PEP would read as follows:

   class A

   class B:
       value: A

   class A:
       value: B

If you're speed-reading here and wondering "wait, how is this not ambiguous?", note that in the first line of the example, the "class" statement has no colon.  Also, it would never have parentheses or decorators.  The forward declaration of a class would always be just "class", followed by a name, followed by a newline (or comment).


//arry/
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/GTOJIZZXON3KND37WSATL3E7Q4KON6YO/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to