Fun with numbers - dammit, but I want a cast!

Carl Banks imbosol at aerojockey.com
Tue Aug 12 00:13:07 EDT 2003


Terry Reedy wrote:
> 
> "Carl Banks" <imbosol at aerojockey.com> wrote in message
> news:yVVZa.764$u%2.33 at nwrdny02.gnilink.net...
>> 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.
> 
> From what I remember both in my own code and others that I have read,
> most casts, where 'most' is measured in frequency of occurence in
> actual code, which is what I meant by 'usually'.

I didn't like the wording.  The way you said it, it sounded (whether
you intended it or not) like you were saying: "C generally casts by
preserving the bit pattern, except for a few exceptions," but that's
not how it is.

Even if they are more commonly used, casts that preserve the bit
pattern are the special ones.  The general rule is that C casts
preserve "value."  Sometimes the equivalent "value" happens to have
equivalent bits: that's the case for int-to-pointer and
pointer-to-pointer.


[snip]
>> 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.
> 
> On which systems?  How widely used?  I also added 'at least usually'
> to account for systems I don't know about ;-)

Don't know of any examples off hand.  The comp.lang.c FAQ lists some.


>> 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
>> themselves.
> 
> In my opinion, it is unhelpful to refer to type-function calls as
> casts.  To me, the baggage 'cast' carries is more confusing than
> helpful.  For instance, a common conversion, between number and
> string, cannot be done in C with a type cast but must also (as in
> Python) be done with a function call.  Would you call C's atoi(),
> atof(), and sprintf casts?  In C, the 'funny syntax' defines what I
> believe most people call a type cast versus a function call.  This is
> certainly true for both K&R and the Standard C committee.

On one hand, I respect that opinion.  OTOH, I think the assertion that
Python has no type casting leads to misunderstanding.  Take the OP for
example.  Because of the statement that Python has no type casts, he
didn't realize that Python does have functionality of type casting.

So I don't think anyone should say simply, "Python does not have type
casts."  Better, I think, to say, yes, Python has type casts, although
we don't call them type casts 'round here because they look and act
like regular function calls.

And to answer your question, no: atoi, atof, and sprintf are not type
casts even by my definition, because string is not a type in C.


-- 
CARL BANKS
"You don't run Microsoft Windows.  Microsoft Windows runs you."




More information about the Python-list mailing list