Re: Some things I'd like

Guido.van.Rossum@cwi.nl
Wed, 10 Aug 1994 14:34:09 +0200

Jack Jansen:
> I'm not sure how useful this idea is, but I'm pretty sure I wouldn't
> like a new keyword. Also, the keyword only allows a single baseclass
> to be specified, so how about
> class b(parent=a):
> def m(self): ... parent.m() ... # Calls a.m()
>
> (although there will probably be scoping rule problems here with the
> current implementation).

Brilliant! Except I think a syntax change isn't even needed. You can
do this now, as follows:

parent = a
class b(parent):
def m(self): ... parent.m(self) ... # Calls a.m()

This essentially defines a symbolic name for b's parent class, so if
you want to change the base class you simply change the assignment to
parent.

If you are defining multiple classes in one module (not unlikely :-),
you need to introduce different variable names for each parent, e.g.

b_parent = a
class b(b_parent):
def m(self): ... b_parent.m(self) ... # Calls a.m()

c_parent = b
class c(c_parent):
.....

Or, if you don't mind writing the base class name twice, you could
make it a class variable:

class b(a):
parent = a
def m(self): ... b.parent.m(self) ... # Calls a.m()

The solution for multiple bases is obvious as well...

--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>
<URL:http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>