[Python-3000] Type parameterization (was: Re: Type annotations: annotating generators)

Talin talin at acm.org
Sat May 20 06:23:20 CEST 2006


Collin Winter wrote:
> On 5/19/06, Guido van Rossum <guido at python.org> wrote:
> 
>>On 5/19/06, Collin Winter <collinw at gmail.com> wrote:
>>
>>>As a quick data point, I showed around your "dict[str, str|int]"
>>>example to some friends, all of whom have done a decent amount of
>>>coding in Python. I gave them some background, including that this was
>>>an example of type parameterization. To quote one fellow, a PhD
>>>student in CS at Edinburgh: "So, uh, 'dict[str, int|str]' is supposed
>>>to represent... uh... I give in, what the hell is that supposed to
>>>denote?"
>>
>>Did you first show them list[int] (to get them used to the idea of
>>parameterized types) and then dict[str, int] (since the int|str union
>>could be a distractor)?
> 
> 
> I showed examples like list[str] and tuple[int, int], but I don't
> think I included dict[str, int].

OK, try this on your friends and see what happens:

    dict[ str >> int ]

and similarly:

    Function( args ) >> int

In other words, I am overloading the right shift operator to mean "maps to".

Alternative suggestion: Define -> as an operator.

    def myfunc( a:dict[ int -> int ], b:Function( int ) -> int ) -> int:
       ...

In English, this means: "A function which takes two arguments, the first 
being a dictionary which given an int produces an int, and the second 
argument, a function which takes an int and produces an int. The 
function itself returns an int."

Side note: I'm actually in favor of the idea of Python adding 
syntactical support for operators that have no "built-in" definition. 
The use case would be for classes that define new operators that don't 
correspond to the semantics of any existing operator. But that's another 
thread, maybe one not worth starting :)

-- Talin


More information about the Python-3000 mailing list