[Python-ideas] pep-0484 - Forward references and Didactics - be orthogonal
Andrew Barnert
abarnert at yahoo.com
Tue Aug 25 23:34:20 CEST 2015
On Aug 25, 2015, at 09:19, Erik Bray <erik.m.bray at gmail.com> wrote:
>
>> On Tue, Aug 25, 2015 at 11:15 AM, Terry Reedy <tjreedy at udel.edu> wrote:
>> 2. Class statements would initially create an empty class bound to the class
>> name. This could break back compatibility, and would require cleanup in
>> case of a syntax error in the body. This would be similar to import
>> statements initially putting a empty module in sys.modules to support
>> circular imports. This is messy and still bug prone is use.
>
> I have been thinking about this lately in a different context, and I
> would very much favor this approach. I think in large part because it
> works this way for modules it would make sense for it to work for
> classes as well. The fact that ClassName is bound to an object that
> will *eventually* become the class as soon as the parser has read in:
>
> class ClassName:
>
> represents, to me (and I would suspect to many students as well), the
> least astonishment.
The problem here is, what if someone writes this:
def __init__(self, left: Tree, right: Tree):
# something with left.left
Or:
@classmethod
def maketree(cls):
return Tree(None, None)
Here, Tree is "defined", but the type checker can't actually infer the type of left.left or the arguments of Tree's constructor (even if __init__ was defined before maketree).
There are various ways you could special-case things to deal with this problem. The simplest would be that a forward-declared class just has no methods or other attributes, or maybe that it has only the ones inherited from superclasses or metaclasses, until the definition is completed, but my naive intuition says that it's obvious what both of the above mean, and the only reason I'd expect it to be an error is by understanding how it has to work under the covers.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150825/4d8e8097/attachment.html>
More information about the Python-ideas
mailing list