[Tutor] Dynamic/Weak Types and large projects

Jeff Shannon jeff@ccvcorp.com
Tue, 30 Jul 2002 10:59:21 -0700


anthony.barker@bmo.com wrote:

>
> 1) Dynamic vs Weak Types
>
> I have heard python referred to as having "weak" types - how does
> this differ from "dynamic types" ?

This is actually exactly the opposite of how most Pythonistas would
describe Python's type system -- Python has *strong* but dynamic
typing.  Dynamic typing means that any name can be bound to any type
of object.

# create an integer
MyVariable = 1
# then replace it with a string
MyVariable = "A String"

Python is perfectly happy with this, but C/C++ would throw fits.
(Well, compiler errors.)  The strong typing means that the operations
you can perform on an object depend upon the type of the object --
Python will (usually) not coerce things from one type to another
unless you specifically ask it to.

>>> x = 1
>>> y = "2"
>>> x + y
Traceback (most recent call last):
  File "<input>", line 1, in ?
TypeError: unsupported operand types for +: 'int' and 'str'
>>>

Here, you cannot add these two variables because they are of different
types.  In Perl, a language that truly is weakly typed, you *would* be
able to add these together and get a result of 3.

The real difference between a dynamically typed language (such as
Python) and a statically typed language (such as C/C++), is that in
static languages, the type is associated with a specific variable name
-- once you create a variable, that name will always be of the type it
was created with.  In dynamic languages, the type is associated with
an *object* -- but a given name can refer to *any* object, regardless
of its type.

The advantage of static typing is that a compiler can ensure that
every operation performed on an object is valid for that type of
object, and that compiler errors give the programmer the earliest
possible warning about problems.  You don't need to test the whole
program in order to find type mismatches.  Of course, this is also the
biggest drawback of static languages -- since you don't need to test
everything for type mismatches, most programmers *won't* test
everything -- and therefore may not uncover logic errors or algorithm
errors that are buried in untested code ("it compiled so it *must* be
right").

The big advantage of dynamic languages is that it encourages generic
programming.  You don't have to worry about what *type* an object is,
only what operations it supports.  This means that if you want to add
a new type, that supports the same operations in a slightly different
way, it is trivially easy -- the client code that uses the objects
probably won't change at all, whereas in a static language you'd at
*least* have to go through and change all of the variable declarations
(and hope that you don't miss any).

Static languages nowadays have quite a range of features that are
designed to enable generic programming styles -- pointer upcasting,
templates, etc.  A lot of work has gone into the C++ standard template
library... which, in effect, allows you to program C++ using the sorts
of idioms that are native to dynamic languages like Python.  But those
idioms are still easier to use in truly dynamic languages.


> 2) General consensus is that for large projects strongly typed
> languages are better.
> Is there any statistical proof of this beside hearsay?

No, there is no proof of this -- or at least, there's no proof that
*statically* typed languages are better (which is what most people
mean when they say "strongly typed" in this context).  In fact, most
of the experimental evidence these days supports the idea that dynamic
langauges are "better" in most senses of the word -- shorter code,
fewer errors, faster release times.  The one area in which static
typing is a demonstrable advantage is in situations where a program
must be provably correct, in the sense of writing a mathematic proof.
Mathematical analysis of a dynamic program is considerably more
difficult.  (However, common-sense interpretation is often easier.)

Jeff Shannon
Technician/Programmer
Credit International