5 ms·
Can you elaborate on this? I’d love to actually use types in Python.
by rytill 7y ago
Can you elaborate on this? I’d love to actually use types in Python.
- acemarke 7y agoPython 3 has optional static typing: https://docs.python.org/3/library/typing.html https://docs.python.org/3/library/typing.html https://realpython.com/python-type-checking/ https://realpython.com/python-type-checking/
- nurettin 7y agoStatic typing is exactly what python doesn't have. It will remain dynamically typed. It has type hints which can be used with external tools to enforce type correctness to some degree, but python code will run even if you assign: def f(x: int): x = "str" f(42)
- joshuamorton 7y agoThis is true of java and c as well. You can cast things or subvert the type system and the code will still run. At least where I work, my build process prevents me from running Python code with invalid type annotations, so it's exactly the same as for Java is cpp or any other statically typed language.
- nurettin 7y agoIn my opinion, the main difference between the two from a developer's perspective is this; You can't grep for dynamic type errors. You can grep for a cast to see where you've made a mistake and find a way to do it without a cast. This of course goes for the named casts in C++. It is harder to grep for casts in C.
- joshuamorton 7y agoRight, but if you run mypy over your code, you're in the same situation as in java. It's a one liner to run mypy before you invoke your tests. You have to cast to subvert the type system. In c you have void ptr everywhere anyway.
- deleted 7y ago[deleted]
- fulafel 7y agoTerminology: static vs dynamic typing != typed vs untyped. You can't not use types in Python. Examples of untyped languages would be B, assembly, Forth(?) - where the system doesn't distinguish what type of objects are in memory or storage locations.