Fun with numbers - dammit, but I want a cast!
imbosol at aerojockey.com
Tue Aug 12 01:46:38 CEST 2003
Terry Reedy wrote:
> No, quite different. A C cast (at least usually) recasts a block of
> bits in a new role without changing the bits.
Certainly not. Most type casts actually change bits. For example,
supposing floats and longs are both 32 bits, the expression (float)1L
will *not* return the floating point number with the same bit pattern
as 1L. It returns 1.0.
Usually, the only casts that preserve the bit pattern are integer to
pointer casts, and the C standard doesn't even guarantee that (unless
C 2000 changed it). In fact, the C standard says (or used to say)
that 0 must always cast to a null pointer, even if the system
represents integer 0 and null pointer with different bits, which does
(or used to) happen.
IMO, a type cast is just a fancy name for an operator that takes an
object and returns an object with the same "value" (whatever that
means) but a different type. In C, type casting happens to have a
funny syntax. In Python, it does not. If someone asked, "does Python
have type casting?", I would say yes, except there's no special syntax
for it. Rather, type casting is done by the calling type objects
"You don't run Microsoft Windows. Microsoft Windows runs you."
More information about the Python-list