[Python-3000] Draft pre-PEP: function annotations

Nick Coghlan ncoghlan at gmail.com
Sat Aug 12 10:13:44 CEST 2006


Collin Winter wrote:
> Return Values
> -------------
> 
> The examples thus far have omitted examples of how to annotate the
> type of a function's return value. This is done like so:
> 
> ::
>     def sum(*vargs: Number) -> Number:
>         ...
> 
> 
> The parameter list can now be followed by a literal ``->`` and
> a Python expression.  Like the annotations for parameters, this
> expression will be evaluated when the function is compiled.

I'd like to request that the annotation for the return type be *inside* the 
parentheses for the parameter list. Why, you ask?

Because, as soon as the annotations are at all verbose, you're going to want 
to split the function definition up so that each parameter gets its own line.

For the parameters, this works beautifully because parenthesis matching keeps 
the compiler from getting upset:

   def sum(seq: "the sequence of values to be added",
           init=0: "the initial value of the total"):
       # do it

But now try to document the return type on its own line:

   def sum(seq: "the sequence of values to be added",
           init=0: "the initial value of the total")
           -> "the summation of the sequence":
       # do it

Kaboom - SyntaxError on the second line because of the missing colon. However, 
if the return type annotation is *inside* the parentheses and separated by a 
comma, there's no problem:

   def sum(seq: "the sequence of values to be added",
           init=0: "the initial value of the total",
           -> "the summation of the sequence"):
       # do it

Having to use a line continuation just to be able to annotate the return type 
on a separate line would be an annoyance.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list