[Python-ideas] Improving the expressivity of function annotations
Carl M. Johnson
cmjohnson.mailinglist at gmail.com
Tue Apr 5 02:44:16 CEST 2011
More information about the Python-ideas mailing list
Tue Apr 5 02:44:16 CEST 2011
- Previous message: [Python-ideas] Improving the expressivity of function annotations
- Next message: [Python-ideas] Improving the expressivity of function annotations
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Mon, Apr 4, 2011 at 9:03 AM, Masklinn <masklinn at masklinn.net> wrote: > Again, the issue is function annotations, which was my context. The example was just to give an idea of the operation performed. In function annotations, unless you went with type parameters and ``tuple[A, B, C]``, ``(A, B, C)`` would likely be interpreted as "a triple of instances of types A, B and C". Yes, but since function annotation don't do anything at present (and maybe never will by default), your function will have to use a decorator to turn on the type checking. At that point, it's just a matter of API design how best to do it. For example, let's say I have a sum function, and I want it to take an iterator containing some type and then return something of the same type. In semi-C-ish style, we'd call this list[int] -> int or list[Generic number] -> Generic number. But since we're making up our own API for type checking, in this case we can make it work however we want. We don't need to have a particular grammar baked into the language. Try something like: from type_checking_annotations import * g = Generic() @typechecked def sum(iterable: Container(g)) -> g: iterator = iter(iterable) total = next(iterator) for item in iterator: total += item return total sum([0, 0.0]) #Raise TypeError: First item in container was type int, subsequent item was type float
- Previous message: [Python-ideas] Improving the expressivity of function annotations
- Next message: [Python-ideas] Improving the expressivity of function annotations
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-ideas mailing list