syntax difference

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Jun 18 10:03:53 EDT 2018


On Sun, 17 Jun 2018 12:07:14 -0700, Jim Lee wrote:

> IMHO, trying to shoehorn static type checking on top of a dynamically
> typed language shows that the wrong language was chosen for the job.


The experience of computer languages like Lisp, Smalltalk/StrongTalk, 
Erlang, Racket, Javascript variant Typescript, and PHP variant Hack (to 
mention only a few) shows that type hinting on top of dynamic languages 
can and does work very well.

As a human programmer, you surely perform your own ad hoc type checking 
when you write and debug code. When reading a function like this:

# from the calender.py module in the standard library
def isleap(year):
    """Return True for leap years, False for non-leap years."""
    return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

naturally you reason that year is probably an integer, not a list of 
strings, and if you see code call:

isleap("hello")

you almost certainly realise it will fail without needing to run the 
code. Having an automated checker or linter do something similar is 
neither rocket science, nor against the spirit of dynamic typing.

Linters have done some type-checking for many years. One popular 
convention was Sphinx-based type annotations in docstrings:

http://www.pydev.org/manual_adv_type_hints.html


Across the programming language landscape, dynamic languages are adding 
static features, and static languages are adding dynamic features:

http://steve-yegge.blogspot.com/2008/05/dynamic-languages-strike-back.html

Since Steve Yegge gave that talk, the process has only accelerated, 
driven by both practical experience with gradual typing and a lot of 
academic research.

PySonar has been successfully used by Google for close on a decade:

https://yinwang0.wordpress.com/2010/09/12/pysonar/

and MyPy has also been very successful:

http://mypy-lang.org/

particular at DropBox, which is (I believe) funding a lot of Guido's time 
on this, because they need it.

It is 2018. People who say that static typing cannot be integrated with 
dynamic languages are nearly half a century behind the state of the art 
in computer programming.

(As an industry, the programming community is *painfully* conservative. 
Things which Lisp was doing in the 1950s are *still* not mainstream, and 
most programmers do not even imagine they are possible.)


-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson




More information about the Python-list mailing list