syntax difference

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Jun 17 12:59:24 EDT 2018


On Sun, 17 Jun 2018 17:19:38 +0100, Bart wrote:

>> Indeed -- there are no variables in Python, if you think of a variable
>> as meaning a virtual box at a fixed memory address, containing a value,
>> and associated with a type, like in C or Pascal.
> 
> So what's a Type Hint associated with in Python?

By the interpreter itself? Pretty much nothing at all. Aside from 
recording the annotation in the function object, the interpreter 
completely ignores it.


py> def func(x: float) -> list:
...     pass
...
py> func.__annotations__
{'x': <class 'float'>, 'return': <class 'list'>}

(Aside: in more recent versions of Python, the annotations are treated as 
strings, not objects, and you will get {'x': 'float', 'return': 'list'} 
instead.)


Since it is a type *hint*, not a type *declaration*, the interpreter can 
and does ignore it. It makes no change at all to the execution model of 
the language.

But the human reader, linters, IDEs and editors can associate it with the 
name it annotates, and use it as a hint as to what is intended to happen, 
and flag any discrepancies.



-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson




More information about the Python-list mailing list