Is Python type safe?

Skip Montanaro skip at pobox.com
Tue Mar 16 16:06:26 EST 2004


    Skip> Python: yes.  I don't know about Java or C# but my guess is that
    Skip> since C++ has casts you can make it do some type unsafe things.

Sorry to follow up to my own post, but unions also create type unsafety.
Unlike Pascal's records (remember them?), C/C++ unions are not
discriminated, so you can write values into the union using one slot and
read using another:

    #include <stdio.h>
    int main(int argc, char *argv[])
    {
        union {
            float f;
            int i;
        } x;

        x.f = 3.14159;
        fprintf(stderr, "%x\n", x.i);
    }

(pause while Skip reboots the one neuron that knows anything about unions
and compiles this to see if he's got it right...) which produces:

    % make foo
    cc    -c -o foo.o foo.c
    cc   foo.o   -o foo
    % ./foo
    40490fd0

Skip

    

    




More information about the Python-list mailing list