4 ms·
> but not that it’s a list of ints Python type annotations do support such use case: List[int].
by netheril96 8y ago
> but not that it’s a list of ints
Python type annotations do support such use case: List[int].
- Jeff_Brown 8y agoOmg really? Sweet! What about functions, and functions of functions, etc? Can I specify that the argument to function f should be some other function g which should receive a list of ints and return a dict of functions with keys that are ints and values that are functions from int to int?
- detaro 8y agoYes, you can do that too.
- Jeff_Brown 8y agoJust tried it. What's the point of type signatures if the compiler won't hold you to the promises you've made? This is in Python 3.6.4: >>> def f (x:int) -> int: return x+1 ... >>> def g (x:str) -> str: return f(x) ... >>> g(1) 2 I expected a complaint after the second definition: I specified that g takes and returns a string, and then defined it to take and return an int. Not only did Python not catch the error at compile time (defining g), it didn't even catch it at runtime (calling g).
- netheril96 8y agoFor some strange reasons Python developers decide to provide the syntax of type annotations but leave the actual checking to third party implementations.
- detaro 8y agoType annotations are just that: annotations. They currently have no meaning to the CPython interpreter and are purely consumed by external tooling (IDEs, there's a static typechecker called mypy, ...) or code that chooses to use them (e.g. there's web frameworks that convert and validate parameter types according to the signature of the handler function)