not safe at all

kosh kosh at aesaeion.com
Thu Jul 12 21:26:18 EDT 2001


Dennis Roark 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.  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?
> ---------------------------------
> Dennis Roark
> Dept. of Computer Science
> University of Sioux Falls
> 
> Starting Points:  http://home.earthlink.net/~denro
> ---------------------------------

What you have posted has nothing to do with typesafety in the slightest. 
Not once did you actually mix types. What you have done is just change the 
assignment. At first you had x equal to an integer of 5, then when you 
assigned it a value of a string it deleted the old object and made a new 
one. Then it did the same as a list. 

What you are really complaining about is that it is easy to reassign a var 
to point to a new object. If you do id(x) after each assignment you will 
see x is not the same object.



More information about the Python-list mailing list