output formatting for user-defined types

Russ uymqlp502 at sneakemail.com
Sat Apr 8 00:18:23 EDT 2006


>>>>> dist = 4 * ft
>>>>> print >> out, dist/ft
>> 4

>> Note, however, that this requires the user to explicity ask for the
>> conversion.

>How is this any more explicit and any less safe than:

>dist = 4 * ft
>print float(dist)

Because the former specifies the actual units and the latter does not.
If the base units were not feet, then the latter would not return the
value in feet (which happens to be 4 here). It would return the value
in whatever the base unit for length happened to be. So the former
works even if the base unit is changed, but the latter does not.

Secondly, allowing unconditional conversion to float allows any unit to
be passed to trig functions. But any unit other than (dimensionless)
radians passed to a trig function is an error. I explaned that already,
but apparently you missed it.

>But in any case, I suspect you do automatically convert units. What do you
>do in this case:

>x = 45*ft
>y = 16*m
>z = x+y

>Do you raise an error?

Nope. Works just fine. You obviously didn't look at the user guide.
What happens is that any length unit is automatically converted to the
chosen base unit for length, so everything is consistent.

>All you are doing is making a rod for your own back, to no advantage.

Wrong again.




More information about the Python-list mailing list