суббота, 18 января 2020 г.

Yet another example of type safety in Python

Untitled

Python can be safe (if we need it) too. Another example is: let's say we want to implement a function unique(). It may looks like:

Truth is that data should be sorted - we don't want to sort it because data items maybe are sorted already. So, instead to write docstring about this our requirement ("data should be sorted") we can specify this requirement as a type, so it will prevent a client from passing of data which was not sorted, so the check will work at compile time:

And if we try to pass something which has not type Sorted then we will get an MYPY error at compile time. It looks like Haskell, ah? :)

Комментариев нет:

Отправить комментарий

Thanks for your posting!