Builtin subclassing idea (Re: General q. `bout subclassing)
greg.ewing at compaq.com
Wed Nov 3 09:37:39 CET 1999
Thomas Wouters wrote:
> I believe the classless-builtin-types problem is supposed to be fixed by
> Python 2.0. But I've seen that in response to so many ideas...
In this case, the problem is that nobody has thought of a way
of implementing it that wouldn't greatly slow down operations on
builtin types, or make builtin types take up a lot more memory,
Mr. Skaller in his Viper project seems to have hit upon a way
of solving part of the problem, allowing methods to be added to
the existing builtin types, and it seems that the same technique
could be used in CPython. It still doesn't let you make subclasses
of the builtin types, however.
Hmmm, I've just had an idea... what if the interpreter were
to recognise when you wrote something like
and behave as though you had instead written
Then you'd be able to at least *pretend* that you can
subclass builtin types...
To implement this in a general way, add a "convert_to_class"
method slot to the TypeObject. Whenever an object which
isn't a class is used as a base class, its convert_to_class
operation would be invoked. For lists and dictionaries
this would import and return UserList or UserDict, and
similar emulation classes could be added for the other
More information about the Python-list