output formatting for user-defined types

Russ uymqlp502 at sneakemail.com
Thu Apr 6 14:05:06 EDT 2006


>Yeah, how about we read your mind or make wild guesses about why it's
>not acceptable, and about what your requirements really are.

>Really, your response seems a little bizarre to me, given that __float__
>is the defined way in which float() gets a value from an instance, and
>float() is what the % operator calls when it encounters a '%f' in the
>format string.

My class represents physical scalars with units. If I allow arbitrary
conversion to float, then the user can pass any units to trig
functions. But trig functions expect radians, so passing any other unit
(e.g., degrees or meters) is an error. To prevent such errors, I allow
conversion to float only when the units are actually radians (or
dimensionless). That is just one of several reasons for preventing
arbitrary conversion to float, but I won't go into the others here.

By the way, I realize that several other classes have been developed
for representing physical scalars, but I think mine is unique in that
it allows the user to easily disable the units for more efficient
"production runs" after testing is complete. This can increase
efficiency by two orders of magnitude.

I announced my application a week or two ago on comp.lang.announce.
Anyone who is interested can download the code and user guide at
http://RussP.us/scalar.htm

Check it out. I bet you'll like it.




More information about the Python-list mailing list