subclassing float (or not)

Jeff Shannon jeff at ccvcorp.com
Tue Feb 26 19:32:39 EST 2002


Steven Majewski wrote:

>  If you
> subclass float instead, you don't have to wait around to see
> what else breaks because you forgot to implement a method to
> support it!

No, but you *do* have to watch carefully for things that "work", but
provide the wrong answer.  In the example of PhysicalValue, I presume
that subclassing float would get it to "automatically" work with, say,
math.sqrt()... except that it'd only work for your scalar, not your
units.  The square root of 100.0(cm**2) is 10.0cm, but the automatic
coercion would give you either 10.0 (a straight float), or 10.0(cm**2).
This particular problem is easy to spot, but who knows that *other*
things you'll be assuming will "automatically" work, but which produce
subtly wrong answers?

(Not that I'm saying that it definately *shouldn't* subclass float --
I'm just saying, don't assume that "doesn't break" == "correct".)

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list