[Python-Dev] Classes and Metaclasses in Smalltalk

Guido van Rossum guido@digicool.com
Wed, 02 May 2001 10:11:17 -0500


> Guido van Rossum wrote:
> > 
> > > This doesn't work in Python since Python has multiple inheritence,
> > > e.g. super in
> > >
> > > class A(B,C):
> > >       def foo(self):
> > >               super.foo()
> > >
> > > is ambiguous.
> > 
> > I'm not sure what you mean.  The search is totally well-defined: first
> > search B for a foo method, then search C.
> 
> I thought you were talking about an abstract super class which is
> how Java uses this term. 

Ah.  I didn't realize.  This would suggest that another (not yet
mentioned) suggestion would be to spell the basemethod call as

    super.foo(self)

keeping more in line with the tradition of passing self explicitly
when calling basemethods.

> Rereading some of the posts, I think you are indeed referring to
> the method which foo overrides -- this is what I call basemethod
> (since it is implemented in one of the base classes).

Aha.

> > > I'd rather suggest adding a function for finding the basemethod
> > > of a method. This is probably the most common task in this context.
> > 
> > I've never heard of the concept of basemethod, but if I may venture a
> > guess, it would be the same definition as I give above.
> 
> The basemethod can be defined as the first method of the same name
> found in the inheritence tree using the standard Python lookup 
> strategy (left-right, depth first) when continuing the lookup search
> at the node in the inheritence tree which defines the method querying
> the basemethod.

Yes, that's what I guessed.

> In other words: you let Python continue the search for the method
> as if it hadn't found the occurrance calling the basemethod()
> API. Hmm, still not clear enough... better let Tim jump in here
> (we've had a discussion about basemethod() some months or years
> ago). Tim ?
> 
> Note that there are many ways of defining what a basemethod
> is, due to the ambiguities that are caused by multiple inheritence
> (e.g. the same base class may appear in different branches of the
> inheritence tree).

Well, the search will find one definite method, but you're right that
there may be situations where it's necessary to specify the specific
base class!

In C++ that is solved by writing B::foo() or C::foo().  Python doesn't
have "::" and instead overloads the "." operator.  Hmm, so even
introducing super doesn't completely remove the need to be able to
write C.foo to reference the unbound method foo of class C, and this
may require that my ugly rule still be needed.

AFAIK, Smalltalk has only single inheritance, and so does Java, so
there 'super' is enough.  Will we need to add a "::" operator to
Python???

--Guido van Rossum (home page: http://www.python.org/~guido/)