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

Stefan Krah skrah at bytereef.org
Fri Aug 28 15:17:54 CEST 2015


Prof. Dr. L. Humbert <humbert at ...> writes:
> It is indeed possible to run the following code
> /
> from typing import List
> class Tree:
>      def __init__(self, left: 'Tree', right: 'Tree'):
>          self.left = left
>          self.right = right
>      def leaves(self) -> List['Tree']:
>          return []
> def greeting(name: 'str') -> 'str':
>      return 'Hello ' + name
> \
> 
> but not
>>      def leaves(self) -> 'List'['Tree']:
>> which would be orthogonal, when deciding to put all used types in '…'
> 
> Perhaps there will be a chance to make this a valid construction?

The issue is that Python does not have separate type/value universes.
'Tree' is just a type hint, not a type in the conventional sense.

I would very much like if Python *did* have separate types/values,
so that one could write (OCaml):

class tree (left : tree) (right : tree) =
  object val left = left
         val right = right
  end
;;


Which is an uninhabited type, since you need a tree to construct
a tree! :)


Thus:

class tree (left : tree option) (right : tree option) =
  object val left = left
         val right = right
  end
;;


Stefan Krah



























More information about the Python-ideas mailing list