[Python-ideas] pep-0484 - Forward references and Didactics - be orthogonal

Sven R. Kunze srkunze at mail.de
Tue Aug 25 19:48:39 CEST 2015


On 25.08.2015 17:15, Terry Reedy wrote:
> On 8/24/2015 12:19 AM, Prof. Dr. L. Humbert wrote:
>
>> What students should be able to code:
>>
>> 1. varinat
>> #-------------wishful----------------------------------\
>> class Tree:
>>      def __init__(self, left: Tree, right: Tree):
>>          self.left = left
>>          self.right = right
>
> As you should know, at least after reading previous responses, making 
> this work would require one of two major changes to Python class 
> statements.
>
> 1. The class name has special (context sensitive) meaning in enclosed 
> def statements.  The compiler would have to compile def statements 
> differently than it would the same def statements not in a Tree class. 
> It would then have to patch all methods after the class is created.  
> See the annoclass function below.
>
> A proposal to make the definition name of a function special within 
> its definition has already been rejected.
>
> 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.

Although, I do not agree with the intentions of the OP, I would love to 
have "more forward references" in Python.


I think the main issue here is the gab between intuition and what the 
compiler actually does. The following line:

class MyClass: # first appearance of MyClass

basically creates MyClass in the mind of the developer reading this 
piece of code. Thus, he expects to be able to use it after this line.
However, Python first assigns the class to the name MyClass at the end 
of the class definition. Thus, it is usable only after that.

People get around this (especially since one doesn't need it thus 
often), but it still feels... different.

Best,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150825/7fc064f6/attachment.html>


More information about the Python-ideas mailing list