Bug or Feature?

Mel Wilson mwilson at the-wire.com
Mon Nov 24 12:03:29 EST 2003


In article <m3llq5zufq.fsf at pc150.maths.bris.ac.uk>,
Michael Hudson <mwh at python.net> wrote:
>Stephan Diehl <stephan.diehl at gmx.net> writes:
>
>> Although, the question was more along the line, if (in the light of the
>> principle of least surprise) this behaviour makes sense.
>> My private opinion would be, that the usefullness of builtin types as real
>> types would be much better, if they were safe under inheritance.
>
>The problem is that it's impossible for the int type to know about the
>requirements of your subclass' constructor.  Explicit is better than
>implicit, and all that.

   Not strictly true.  A numeric operation can access the
objects class, as:

    def __mul__ (self, other):
        if not isinstance (other, Decimal):
            other = Decimal (other)
        return self.__class__ (self._v * other._v, self._e + other._e)

so that operations on a descendant of the Decimal class will
return objects of the descendants class.  (Strange that I
didn't code `isinstance (other, self.__class__)`, etc.  Must
think out the consequences of that.)

   What this would mean in runtime for the bazillions of int
operations that run every day, I don't know.

>For the example you posted, I don't see much of an advantage to
>inheriting from int.

   True.  With int working as it does, it might be better to
pull out all the stops and write an Int class that supports
full inheritance, and work from there.

        Regards.        Mel.




More information about the Python-list mailing list