On 4/22/22 19:36, Terry Reedy wrote:
On 4/22/2022 9:13 PM, Larry Hastings wrote:
     forward class X()

New keywords are a nuisance.  And the proposed implementation seems too complex.

My proposed implementation seemed necessary to handle the complexity of the problem.  I would welcome a simpler solution that also worked for all the same use cases.


How about a 'regular' class statement with a special marker of some sort.  Example: 'body=None'.

It's plausible.  I take it "body=None" would mean the declaration would not be permitted to have a colon and a class body.  So now we have two forms of the "class" statement, and which syntax you're using is controlled by a named parameter argument.

I think this "body=None" argument changing the syntax of the "class" statement is clumsy.  It lacks the visibility and clarity of "forward class"; the keyword here makes it pretty obvious that this is not a conventional class declaration.  So I still prefer "forward class".

In my PEP I proposed an alternate syntax for "forward class": "def class", which has the feature that it doesn't require adding a new keyword.  But again, I don't think it's as clear as "forward class", and I think clarity is vital here.


Either __new__ or __init__ could raise XError("Cannot instantiate until this is continued.", so no special instantiation code would needed and X could be a real class, with a special limitation.

Yes, my proposal already suggests that __new__ raise an exception.  That's not the hard part of the problem.

The problem with X being a "real class" is that creating a "real class" means running all the class creation code, and a lot of the class creation code is designed with the assumption that the namespace has already been filled by executing the class body.

For example, Enum in enum.py relies on EnumMeta, which defines __new__, which examines the already-initialized namespace of the class you want to create.  If you propose a "body=None" class be a "real class object", then how do you declare a class that inherits from Enum using "body=None"?  Creating the class object will call EnumMeta.__new__, which needs to examine the namespace, which hasn't been initialized yet.

Changing the class object creation code so we can construct a class object in two steps, with the execution of the class body being part of the second step, was the major sticking point--and the source of most of the complexity of my proposal.


     continue class X:
         # class body goes here
         def __init__(self, key):
             self.key = key

'continue' is already a keyword.

I'm aware.  I'm not sure why you mentioned it.


Given that X is a real class, could implementation be X.__dict__.update(new-body-dict)

That's what the proof-of-concept does.  But the proof-of-concept fails with a lot of common use cases:

 * metaclasses that override __new__ or __init__
 * base classes that implement __init_subclass__

because these methods assume the namespace of the class has already been filled in, but it doesn't get filled in until the @continue_() class decorator.  Handling these cases is why my proposal is sadly as complex as it is, and why in practice the proof-of-concept doesn't work a lot of the time.


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

Reply via email to