Exheritance and Python
ji at tarzan.it.jyu.fi
Fri Jun 14 10:50:39 CEST 2002
ECOOP 2002 had an interesting workshop on inheritance -- whish
I could have attended. It had a particularly interesting paper
about exheritance -- well, I'm a bit tempted to read Mr Sakkinen's
Exheritance is like inheritance, but other way around. As inheritance
specialises, exheritance generalises. If you, my humble reader, can't
think of any use for exheritance, please feel free to skip the rest
of the message.
My first idea after reading the paper was 'well, it should be possible
to exherit in Python', and after a bit of testing, I found out that
Python actually can do exheritance better than I thought...for old
type classes... :( (Old type class is class which does not inherit
So, for an old type class C one can say:
and after that, even subclasses of C find methods of A.
(If you read Sakkinen's paper, hold on. I'm now just considering
using exheritance as 'interface exheritance' or even bare 'type
inheritance', that is, I'm not yet trying to exherit any methods
For new type classes, those that inherit object, this doesn't work,
as __bases__ is read-only. Why? (Answering my own questions, PEP252
tells that "The introspection API is a read-only API", but continues
"A future PEP may define some semantics for some such assignments".
It seems I'll be to writing a PEP. PEP253 also has an innocent open
issue "assignment to __dict__, __bases__".)
I found an article posted here some time ago, which suggested using
type() to re-define class, like:
C=type(C.__name__, C.__bases__+(A,), C.__dict__)
but for two reasons, this does not work:
1. C.__dict__ is a proxy to dict, not a dict object.
2. If there are classes using old class C, they hold
references to that class, but new classes inherited
from C refer to new C. This is clear, type() doesn't
change the old class, it creates a new one 'overrides'
the name of the old class.
So, is there a way to change the __bases__-attribute of a new type
class? Am I to write a PEP? Is anyone interested contributing? Or am
I just to use the old type of classes?
More information about the Python-list