[Python-ideas] Conventions for function annotations

David Townshend aquavitae69 at gmail.com
Thu Dec 6 08:23:31 CET 2012


On Wed, Dec 5, 2012 at 8:17 PM, Guido van Rossum <guido at python.org> wrote:

> On Tue, Dec 4, 2012 at 1:37 AM, David Townshend <aquavitae69 at gmail.com>
> wrote:
> > Just thought of a couple of usages which don't fit into the decorator
> model.
> > The first is using the return annotation for early binding:
> >
> >     def func(seq) -> dict(sorted=sorted):
> >         return func.__annotations__['return']['sorted'](seq)
>
> You've got to be kidding...
>

> > Stangely enough, this seems to run slightly faster than
> >
> >     def func(seq, sorted=sorted):
> >         return sorted(seq)
> >
> > My test shows the first running in about 0.376s and the second in about
> > 0.382s (python 3.3, 64bit).
>
> Surely that's some kind of random variation. It's only a 2% difference.
>

It's consistent.  I ran several tests and came out with the same 2%
difference every time.


> IOW, this is not a line of thought to pursue.
>
>
I wasn't suggesting that this is a good idea, I was merely trying to point
out that there are currently ways of using annotations beyond type
declarations with decorators, and that there may be other use cases out
there which will work well.  Documenting recommendations that annotations
only be used with decorators, or only be used for type declarations will
limit the possibilities because nobody will bother to look further, and if
they do, the ideas will no doubt be shut down as being bad style because
they go against the recommended usage.  I thought that limiting annotations
like this was what you wanted to avoid?

Having said that, I've never found a good use for annotations in my own
code, so I'm not emotionally invested one way or the other.  I do think
that the best usage I've seen is exactly what is being discussed here and
it would be great if there was some prescribed use for annotations.
Perhaps people would actually use them then.

David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20121206/73393b3c/attachment.html>


More information about the Python-ideas mailing list