Re: super() doesn't get superclass



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.
.



Relevant Pages

  • Re: super() doesnt get superclass
    ... After banging my head against super() trying to reliably get ... Return the superclass of type. ... Why does the documentation of 'super' say that it returns the ... currently implemented is in Python at all. ...
    (comp.lang.python)
  • super(...).__init__() vs Base.__init__(self)
    ... The super() method only works correctly in multiple inheritance when the base classes are written to expect it, so "Always use super" seems like bad advice. ... Another fix is for Base.__init__to call super.__init__and to list Base first in the list of base classes. ... This might break existing code that is written in the style of fix 1 (calling both base class __init__() methods explicitly). ...
    (comp.lang.python)
  • Re: super() doesnt get superclass
    ... After banging my head against super() trying to reliably get ... it causes the next method in the MRO to be called. ... Return the superclass of type. ... break the inheritance chain, but this is no different than calling ...
    (comp.lang.python)
  • Re: super() doesnt get superclass
    ... agree that the documentation for super is somewhat misleading (and ... necessarily invoke the superclass of the *implementation* of the ... For example, given a random class: ... ....there is in fact no guarantee that supercalls a superclass of ...
    (comp.lang.python)
  • Re: Copying objects and multiple inheritance
    ... some of the instances in the hierarchy ... copies they need some information updated a little differently), so how can I make sure all the information is copied as it is supposed to be even for the base classes that have special requirements. ... All classes must be written with cooperation in mind, using super() the "right" way. ... That said, and since multiple inheritance is the heart of the problem, maybe you can redesign your solution *without* using MI? ...
    (comp.lang.python)