Adding static typing to Python

Justin Sheehy justin at iago.org
Wed Feb 20 12:12:10 EST 2002


gbreed at cix.compulink.co.uk writes:

>> It also has dynamic typing, as opposed to static typing.  This seems
>> to be the thing that you're objecting to.  

> Do you have a reference for this terminology?  I haven't seen it outside 
> of comp.lang.python. 

Those terms are used fairly often in programming language
discussions.  Many languages use the static/dynamic distinction to
describe their own typing system.

Most university courses on the principles of programming languages
(and decent compilers courses that cover typechecking) talk about the
static/dynamic distinction.

>From the canonical or otherwise respectable web pages about a few
programming languages (not Python-related):

"Eiffel is statically typed to ensure that errors are caught at compile
time, not run time."

"A Java compiler enforces a syntactic discipline on program text
called static typing."

"Many languages provide dynamic typing: Smalltalk, Self, Objective-C,
and etc.  A limited dynamic typing scheme, called RTTI (Run Time Type
Identification), is even being considered for the C++ standard."

"Smalltalk is an example of a dynamically-typed language."

> The only definition I have to hand contradicts it:
> <http://wombat.doc.ic.ac.uk/foldoc/foldoc.cgi?query=strong+typing>

So, if we're just going to pick definitions off the web, here's one
that appeared rather quickly on google:

(http://www-lp.doc.ic.ac.uk/UserPages/staff/ft/alp/net/typing/strong.html)

  Strongly typed: A strongly typed program cannot 'go wrong' (in
  Milner's words) - that is, dump core, produce a wrong answer or
  similar stuff. Note that runtime errors are still possible, but are
  clearly marked as such. (Such as accessing arrays out of the declared
  bounds, which is seldom statically checked.) (Examples: Lisp, SML,
  Goedel, Prolog.)

  Weakly typed: No such guarantee is given. (Example: C.)

  Statically typed: Type checking is done at compile-time. (SML, C, Goedel)

  Dynamically typed: Type checking is done at runtime. (Lisp, Prolog)
  Someone should probably define polymorphism (esp. parametric versus ad
  hoc in all its varieties) as well.

A common interpretation/explanation of this is that dynamic typing
assigns types to values, while static typing assigns types to
variables.  This is in line with my statement about Python's strong
yet dynamic typing.

> Taking "static typing" literally would probably mean that an object 
> couldn't change it's type after creation.  That's almost always the case 
> in Python.  If you mean identifiers have to stay bound to the same type, 
> it'd mean
>
> x = 1
> x *= 5L
>
> would be illegal, because x begins as an integer and then becomes a long.  

This shows that you don't yet understand Python's assignment rules.
In the above, no object is changing type.  The following is what happens:

 - An object of type integer is created.
 - The name "x" is bound to that object.
 - An object of type long is created.
 - The name "x" is bound to that object.

The binding of names to objects is completely independent from the
values of the objects.  Objects have types in dynamically-typed
languages, but names may not.

> So, um, what actually is the problem?

I have no idea.  I'm fairly comfortable with dynamically-typed
lanaguages, so I haven't seen a problem here yet.  <wink>

-Justin

 





More information about the Python-list mailing list