[Python-ideas] parameter annotation semantics

Stefan Behnel stefan_ml at behnel.de
Sun Apr 17 08:26:13 CEST 2011


Bruce Leban, 17.04.2011 06:53:
> Subject was: Re: [Python-ideas] Simple class initialization
> On Sat, Apr 16, 2011 at 6:27 PM, Steven D'Aprano wrote:
>> Bruce Leban wrote:
>>> One
>>> of the strange aspects (to me) of parameter annotations is that they have
>>> no semantics which opens them up to multiple conflicting uses.
>>
>> That's not a bug, that's a feature.
>>
>> It's been stated many times by Guido that it's far too early to standardize
>> on a single meaning for annotations. (We may *never* standardize on a single
>> meaning.) Instead, it is up to the library or decorator to impose whatever
>> meaning makes sense for that particular library or decorator.
>
> I understand that. I can still think it a bit strange, can't I? To be more
> specific, you hit the crux of the problem with the statement "it is up to *
> THE* library or decorator to impose whatever meaning makes sense" [emphasis
> added] is that it assumes the singular. If I want to use two decorators
> which impose different meanings, I'm stuck. Imagine I have two decorators
> like this:
>
> @memoize  # memoizes return values; if memo_value annotation is used,
> memoizes using
>            # the value returned by applying the function
> def a(foo:memo_value(int), bar:memo_value(tuple)):
>    pass
>
> @log_me  # logs function calls and return values excluding parameters
> annotated no_log
> def b(foo, bar:no_log):
>    pass
>
> Can I use both @memoize and @logging?

FWIW, the implementation in Cython accepts tuples as annotation values and 
extracts relevant type annotations from suitable values in them. That was 
chosen because a tuple appeared to be the only reasonable format at the time.

Stefan




More information about the Python-ideas mailing list