Is Python type safe?
Roy Smith
roy at panix.com
Tue Mar 16 17:13:48 EST 2004
In article <mailman.49.1079471195.742.python-list at python.org>,
Skip Montanaro <skip at pobox.com> wrote:
> 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);
> }
Of course you can. Given C's roots as a high level assembly language,
why would you expect anything else?
I was once asked on an interview how I would tell, inside of a C
program, if I was on a big endian or a small endian machine. I said I'd
create a union of a long and a char[4], set the long equal to 1, and see
which char it showed up in. The interviewer looked shocked, thought
about it for a while, and finally said something like, "yeah, I guess
that would work".
To this day, I have no idea what other plan he had in mind.
More information about the Python-list
mailing list