Bug or Feature?

Michael Hudson mwh at python.net
Mon Nov 24 12:56:55 EST 2003


mwilson at the-wire.com (Mel Wilson) writes:

> 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.

Um, did you actually read what I said?  Again, with emphasis:

> >The problem is that it's impossible for the int type to know about the
> >*REQUIREMENTS OF YOUR SUBCLASS' CONSTRUCTOR*.

>    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.

Yes, but now you're constrainting future subclasses' constructors.

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

There would be ways to avoid that, I think.

> >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.

I think someone has written such a UserInt somewhere...

It seems to be generally good advice to only subclass builtin types in
Python if you want to ADD behaviour, not change it.  Not 100% true,
but close.

Cheers,
mwh

-- 
  LINTILLA:  You could take some evening classes.
    ARTHUR:  What, here?
  LINTILLA:  Yes, I've got a bottle of them.  Little pink ones.
                   -- The Hitch-Hikers Guide to the Galaxy, Episode 12




More information about the Python-list mailing list