Python – Why python doesn’t provide optional types

python

I've recently used typescript, and I think the ability to mention or not types is great. It greatly reduces the debugging time while also giving you the advantages of an untyped language. I could imagine a python language superset with the same functionalities, I'm sure the type specifications could be used to greatly speed up the runtime while reducing the debugging hustle. What do you think?

Best Answer

I don't know whether TypeScript enforces type constraints at compile or run time. But Python does support type hints.

Python 3.0 introduced function annotations as defined in PEP 3107. They work like this:

def log_in(login: 'Login', password: 'Password') -> 'Return value':
    # ...

log_in.func_annotation['login'] # => 'Login'
log_in.func_annotation['password'] # => 'Password'
log_in.func_annotation['return'] # => 'Return value'

You can annotate the parameters and the return value with any object.

This can be used to implement type hints. Python 3.5 introduces the typing module. You can implement a decorator that'd perform run-time type checking.

Related Topic