Static typing implementation for Python (was: Strong typing implementation for Python)

Ben Finney ben+python at benfinney.id.au
Sat Oct 10 03:40:43 EDT 2015


John Michael Lafayette <johnmichaelreedfas at gmail.com> writes:

> I would like Python to have a strong typing feature that can co-exist
> with the current dynamic typing system.

You have incorrectly conflated two separate matters.

The opposite of string typing is weak typing. Python has strong typing:
its objects will only behave according to the type they're defined as,
and won't pretend to be what they're not.

The opposite of dynamic typing is static typing. Python has dynamic
typing: references are not at all restricted in what type of object they
reference.

> I would like Python to also be able to also do this:
>
>     Animal a = Factory.make("dog")    # okay. Dog is Animal.
>     Dog d = Factory.make("dog")         # okay. Dog is Dog.
>     Cat c = Factory.make("cat")           # Runtime error. Dog is not Cat.

By that example, you appear to want static typing: references are
restricted to a particular type and cannot refer to other types.

The two ends of that spectrum are pretty much incompatible: to gain
static typing, you must lose dynamic typing.

That does not strike me as an improvement for Python. There are plenty
of statically-typed languages; why would you want Python to lose its
dynamic typing?

> With a strong typing option that performs runtime type checking, the
> reader can be certain that the program is running the type they
> expect. Also, the IDE can be more helpful.

Type annotations <URL:https://www.python.org/dev/peps/pep-3107/> and
type hints <URL:https://www.python.org/dev/peps/pep-0484/> are both in
Python already.

They are entirely optional: code which does not use them can ignore them
completely. However, they appear to provide the benefits you describe,
without losing any of the power of dynamic typing.

-- 
 \              “When cryptography is outlawed, bayl bhgynjf jvyy unir |
  `\                                              cevinpl.” —Anonymous |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list