Is Python type safe?

Erik Max Francis max at alcyone.com
Tue Mar 16 20:34:38 EST 2004


Roy Smith wrote:

> I can't believe I forgot about that!

Well, pointer arithmetic is certainly a different class of operations
than was being talked about here, so literally it's true that you can
perform the addition but semantically it has a totally different and
unrelated meaning than addition between strings and integers in other
languages.

> Of course, if we're going to
> play
> those games, there's always:
> 
> Roy-Smiths-Computer:play$ cat sub.c
> main ()
> {
>     printf ("%d\n", "eight" - "sixteen");
>     exit (0);
> }
> Roy-Smiths-Computer:play$ ./sub
> -8

Cute, but this actually invokes undefined behavior.  Pointer difference
is only meaningful when you're subtracting two pointers that both
reference the same (possibly aggregate) object (or one past the end of
the array).  It happens to give some weird number here; according to the
Standard the program could reasonably crash (and on, say, segmented
architectures, it's far more likely something bizarre would happen).

-- 
 __ Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
/  \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Liberty without learning is always in peril; learning without
    liberty is always in vain. -- John F. Kennedy



More information about the Python-list mailing list