Tuesday, February 10, 2009

Python's Use of Dynamic Typing

An important difference between ABC and Python is the general flavor of the type system. ABC is statically typed which meant that the ABC compiler analyzes the use of types in a program and decides whether they are used consistently. If not, the program is rejected and its execution cannot be started. Unlike most statically typed languages of its days, ABC used type inference (not unlike Haskell) instead of explicit type declarations as you find in languages such as in C. In contrast, Python is dynamically typed. The Python compiler is blissfully unaware of the types used in a program, and all type checking is done at run time.

Although this might seem like a large departure from ABC, it is not as different as you might imagine. Unlike other statically typed languages, ABC doesn't (didn't? it's pretty much purely historic now :-) exclusively rely on static type checking to keep the program from crashing, but has a run-time library that checks the argument types for all operations again each time they are executed. This was done in part as a sanity check for the compiler’s type-checking algorithms, which were not fully implemented in the initial prototype implementation of the language. The runtime library was also useful as a debugging aid since explicit run time type checking could produce nice error messages (aimed at the implementation team), instead of the core dumps that would ensue if the interpreter were to blindly go ahead with an operation without checking if the arguments make sense.

However, the most important reason why ABC has run time type checking in addition to static type checking is that it is interactive. In an interactive session, the user types ABC statements and definitions which are executed as soon as they are completed. In an interactive session, it is possible to create a variable as a number, delete it, and then to recreate it (in other words, create another variable with the same name) as a string. Inside a single procedure, it would be a static typing error to use the same variable name first as a number and then as a string, but it would not be reasonable to enforce such type checking across different statements entered in an interactive session, as the accidental creation of a variable named x as a number would forever forbid the creation of a variable x with a different type! So as a compromise, ABC uses dynamic type checking for global variables, but static type checking for local variables. To simplify the implementation, local variables are dynamically type checked as well.

Thus, it is only a small step from ABC’s implementation approach to type checking to Python’s approach--Python simply drops the compile-time type checking completely. This is entirely in line with Python’s “corner-cutting” philosophy as it’s a simplification of the implementation that does not affect the eventual safety, since all type errors are caught at run time before they can cause the Python interpreter to malfunction.

However, once you decide on dynamic typing, there is no way to go back. ABC’s built-in operations were carefully designed so that the type of the arguments could be deduced from the form of the operation. For example, from the expression “x^y” the compiler would deduce that variables x and y were strings, as well as the expression result. In Python, such deductions cannot generally be made. For example, the expression “x+y” could be a string concatenation, a numeric addition, or an overloaded operation on user-defined types.

6 comments:

  1. That bracket that ends with the smiley in the second paragraph looks mismatched (http://xkcd.com/541/) - no wonder Randall Munroe has been banned from pycon

    ReplyDelete
  2. For what it's worth, there are a number of statically typed languages that have an interpreter - they tend to compile each line at a time, and allow symbols to be redefined. Scala and Haskell both work this way, and I imagine Ocaml and F# do too.

    ReplyDelete
  3. Interesting to learn that Python intentionally abandoned type inference, especially when newer "Python-like" languages like Boo go back to using it.

    I certainly appreciate consistent punctuation (unlike functional languages) and not cluttering things up with type definitions (like classic statically typed imperative languages), plus there are benefits like duck typing.

    ReplyDelete
  4. It does effect "eventual safety", type errors not found at compile time are just shifted to run time. The dynamic nature that made me fall in love with Python for it allowed rapid prototyping is what keeps me worried now in production code base. But this of course can be mitigated by strong unittest coverage and/or assert runtime checks.

    ReplyDelete
  5. I like it. Many static language programmers take "it compiles" as adequate testing, this is pretty much never true. Going all in on dynamic typing removes that option.

    ReplyDelete