type hinting backward compatibility with python 3.0 to 3.4
Steve D'Aprano
steve+python at pearwood.info
Sat May 20 03:53:34 EDT 2017
On Sat, 20 May 2017 11:42 am, Gregory Ewing wrote:
> Steve D'Aprano wrote:
>> On Fri, 19 May 2017 11:35 pm, Edward Ned Harvey (python) wrote:
>>
>>> I *thought* python 3.0 to 3.4 would *ignore* annotations, but it
>>> doesn't...
>>
>> Why would you think that?
>
> Ever since Guido retconned the purpose of annotations to be
> for static type hinting *only*, it would make more sense for
> the interpreter to ignore them, or at least not evaluate them
> immediately at run time (since it would avoid all the problems
> of forward references, etc).
You mean treat them as syntactically comments?
def function(arg:I can put ***ANYTHING*** I like here!!!):
...
I don't think that's a good idea.
Ever since they were introduced, annotations have always been evaluated and
recorded in the function.__annotations__ attribute. That makes them
available at runtime for decorator to do additional processing, or for
introspection.
> So I can see how someone relying on the principle of least
> surprise might assume that.
Oh I don't know, given that everything else apart from #comments is
evaluated at runtime[1] in Python, I think it would be surprising if
annotations weren't.
[1] Apart from some constant folding done as an optimization.
--
Steve
Emoji: a small, fuzzy, indistinct picture used to replace a clear and
perfectly comprehensible word.
More information about the Python-list
mailing list