not safe at all

Brian Quinlan brian at sweetapp.com
Fri Jul 13 04:00:22 EDT 2001


Dennis 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?

I'm sure that most people you read this group/list are well aware of
this behavior :-)

I'm also pretty sure that you're conceptual model of what is going on
needs a bit of rejigging. When you write:

a = 5
a = "Test"

You are not saying that you want to somehow mangle a string so it fits
inside an integer; you are saying that the name "a" should be rebound to
the string "test". And names don't have any notions of what they can and
cannot be bound to. So Python typing is dynamic. But it is also
reasonably strong because most operations require a narrow range of
types to not raise exceptions, as you noted.


Cheers,
Brian






More information about the Python-list mailing list