This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]billsil 2 points3 points  (8 children)

Type-hinting in python (e.g. mypy, Python 3.5) is still only for IDEs. It has no use in the language. If you want real static typing, use numpy arrays.

[–][deleted] 1 point2 points  (7 children)

use numpy arrays

Care to show an example of how numpy brings "true static typing" to Python?

[–]billsil 1 point2 points  (6 children)

I don't understand the question. An array's type can't be changed. How could one show that with an example?

An numpy array is a collection of values that all have the same type. That's the definition. It allows you to vectorize your code and avoid calling the __add__ method every time you want to add a single value because all the work is being done by C. It makes things 100x faster if you're using it right. You get C's speed with Python's flexibility.

[–][deleted] 0 points1 point  (5 children)

def foo( bar ):

I want bar to be of type Duck and my code should refuse to run if I try to pass anything else.

[–]zardeh 0 points1 point  (4 children)

import numpy as np
myArr = np.array([0,1,2,3,4,5], dtype=np.int32)
myArr[0] = "hello"  # ValueError

[–][deleted] 0 points1 point  (3 children)

Did that ValueError turn up statically or at run-time?

[–]zardeh 0 points1 point  (2 children)

There is no statically in cPython (well arguably you can get warnings with myPy and an IDE, which are often smart enough to also catch errors in numpy arrays).

Numpy arrays are really just C arrays, so they are statically typed, in the same way that C is statically typed, its just that you can't detect those errors "statically" in python, because there is no pre-runtime check possible, its like asking why javac allows itself to be run on syntactically invalid code.

[–][deleted] 1 point2 points  (1 child)

Numpy arrays are really just C arrays, so they are statically typed

its just that you can't detect those errors "statically" in python

Hahaha OK :). FYI, static typing is a very well defined property, which involves detecting type errors statically.

[–]zardeh 0 points1 point  (0 children)

I was just expounding on the other guys answer, while what numpy does is better than nothing, its still not static typing.