[Python-3000] Draft pre-PEP: function annotations (original) (raw)

Collin Winter collinw at gmail.com
Thu Aug 10 03:02:08 CEST 2006


On 8/9/06, Talin <talin at acm.org> wrote:

Collin Winter wrote: > There is no worry that these libraries will assign semantics at > random, or that a variety of libraries will appear, each with varying > semantics and interpretations of what, say, a tuple of strings > means. The difficulty inherent in writing annotation interpreting > libraries will keep their number low and their authorship in the > hands of people who, frankly, know what they're doing.

I find this assumption extremely dubious.

Why? This is something Guido and I have discussed and agreed on.

What's your reasoning?

> In pseudo-grammar, parameters now look like > identifier [: expression] [= expression]. That is, type > annotations always precede a parameter's default value and both type > annotations and default values are optional. Just like how equal > signs are used to indicate a default value, colons are used to mark > annotations. All annotation expressions are evaluated at the time > the function is compiled.

Only one annotation per parameter? What if I want to specify both a docstring and a type constraint?

If the grammar were something like identifier [: expression]* [= expression] instead, it would be possible to add multiple annotations to parameters. But what of the return value? Would you want to write

def foo() -> Number -> "total number of frobnications": ...

I wouldn't.

The way to make this explicit, if you need it, would be something like this:

def bar(a: ("number of whatzits", Number)) -> ("frobnication count", Number):

then use a decorator to determine which annotation-interpreting decorators are assigned which annotations, something like this, perhaps:

@chain(annotation_as_docstring, annotation_as_type) def bar(a: ("number of whatzits", Number)) -> ("frobnication count", Number):

Collin Winter



More information about the Python-3000 mailing list