DEV Community

Discussion on: Get started with type hints in Python

Collapse
 
brad profile image
BrandonKMLee

Question: Does type hints increase the speed of the program (lack of need to do lazy type conversion), or does it only increase the speed of debugging?

Collapse
 
decorator_factory profile image
decorator-factory

Annotations don't do anything at runtime. Your program will be just as fast with type hints. What do you mean by "lazy type conversion"?

Collapse
 
decorator_factory profile image
decorator-factory

Annotations don't do anything at runtime

Well, that's not entirely true. You can inspect those annotations:

>>> def f(x: int, y: str) -> float:
...     pass
... 
>>> f.__annotations__
{
    'x': <class 'int'>,
    'y': <class 'str'>,
    'return': <class 'float'>
}
Enter fullscreen mode Exit fullscreen mode

that's why they were added to the language in the first place — type checking wasn't the only goal.
But the interpreter doesn't do anything with the annotations on its own.

Thread Thread
 
brad profile image
BrandonKMLee • Edited

Fair response, so it is more like an error catching tool.

What do you mean by "lazy type conversion"

Sorry, was thinking of Javascript, as Python does not do that.
programiz.com/javascript/type-conv...
sitepoint.com/automatic-type-conve...