super() doesn't get superclass

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Wed Sep 19 23:13:51 EDT 2007


On Thu, 20 Sep 2007 12:00:40 +1000, Ben Finney wrote:

> In its latter form, it is worthless to me when I'm looking for "get
> superclass of A", but its name and parameters and documentation all lead
> me very strongly to believe otherwise.

Why are you looking for the superclass of A?

If it is specifically for the purpose of inheritance, then surely "which 
class(es) is/are the superclass(es)" is an implementation detail that you 
shouldn't care about?

In other words, in principle you want to do something like the following:

class MyClass(*base_classes):
    def method(self, *args):
        print args
        return inherit_from_base_classes(self, 'method')(*args)
        # could also be written as: self.__inherit__('method', *args)
        # or even: self.__inherit__().method(*args)
        # or similar.

The details of the inheritance are not important, so long as it calls the 
right method of the right base-classes in the right order. You shouldn't 
need to know what that order is (except to the extent you've defined the 
base classes).

If that's what you want, then you don't need the class itself. You want 
*something like* super(), even though the existing implementation of 
super is sadly confusing and hard to use.

BUT I think, as far as I can tell, that super() does actually do the 
right thing, *if* you can work out just what arguments to give it, and 
provided all the base classes *and their bases classes* themselves also 
call super().

If you actually want the super-class(es) themselves, heaven knows why, 
then you can use MyClass.__base__ and MyClass.__bases__, although you 
have to intuit this from communing with the cosmos, because dir(MyClass) 
doesn't show them.



-- 
Steven.



More information about the Python-list mailing list