not safe at all

Nick Mathewson 9nick9m at alum.mit.edu
Fri Jul 13 11:01:00 EDT 2001


On Fri, 13 Jul 2001 07:32:28 GMT, Dennis Roark <denro at earthlink.net> wrote:
>For amusement, run this little script which demonstrates a
>near ultimate in the lack of type safety in the language.
>(Perhaps there is a bit of type safety in that you can't do
>this:  4 + "one") But look at what you can do in the
>following script:
>
>x = 3
>x = x + 2
>print "x =", x
>x = "now I'm a string"
>print x
>x = [ 5, x ]
>print "and now a list:", x
>
>If you don't believe it, run it.

I believe it, but it doesn't demonstrate a thing about type safety.
You've just discovered dynamic typing.  You see, your implied view of
typing seems to come from a statically typed language, where every
*variable* has a type of its own.

In Python, types are associated with *objects*, not with *variables*.
IOW, all variables are of the same type: reference-to-object.  

But type safety isn't the same as static typing.  Type safety is
(IIRC) is the property that keeps you from doing stuff like this:
     // C code: no type safety. 
     int x = 3;
     int *xp = &x;
     puts( (char*)xp ); // This is dangerous, but C won't stop you.

     // Java code: type safety.
     Integer x = new Integer(3);
     System.out.println( (String) x ); // Java throws an exception here.

So by the standard definition, Python is of course type-safe.

>                                 Should any language allow
>x to be such a chameleon, to where it can even be a
>different type on two sides of an assignment?	

Once again, this is called dynamic typing, and it's more common than
you seem to think.  It's at least as old as Lisp:
  (let                 ; (ok, is actually scheme.)
    ((x 3))
    (set! x (+ 2 x))
    (set! x "now I'm a string")
    (set! x (list 5 x))
  )

And really, you can write the same code in Java:
  Object x = new Integer(3);
  x = new Integer(2+((Integer)x.intValue()));
  x = "now I'm a string";
  x = new Object[]{new Integer(5), x};

Or Perl, of course:
  $x = 3;
  $x += 2;
  $x = "now I'm a string";
  $x = [ 5, $x ];

Or even C++:
  // Watch out! My C++ is rusty.
  class Integer { int val; 
                  public: Integer(int v) : val(v) {}
                  operator int() { return val; } }
  void* x = new Integer(3);
  x = new Integer(2 + *(Integer*)x);
  x = new string("now I'm a string");
  x = new vector<void*>(2, x);  
  // Ouch!  My brain hurts.  And I just leaked a bunch of memory. :)

In Perl and Lisp, you can see that variables themselves are
untyped[*], as in Python.  In the Java and C++ examples, variables are
typed, but there are also a catch-all types (Object and void*) that
allow the same kind of dynamic behavior.

This is not a Pythonic innovation.  But it is a feature. 

Stop-me-before-I-start-looking-for-a-way-to-do-it-in-CLU-ly Yrs,

[*] It's more complicated than that in Perl, naturally.  It always is.

-- 
 Nick Mathewson    <9 nick 9 m at alum dot mit dot edu> 
                     Remove 9's to respond.  No spam.



More information about the Python-list mailing list