mutlifile inheritance problem

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Sep 18 22:05:26 EDT 2013


On Wed, 18 Sep 2013 20:38:10 -0400, Ned Batchelder wrote:

> super() takes a class and an instance for a reason. If you could use
> self.__class__ for the class, then it would only take the instance.
> Super() needs to know the instance, but also needs to know the class
> it's being called from.

You're correct, of course, but I would word the reason why slightly 
differently.

super needs to know which instance it is being called from, but it also 
needs to know which class the method is defined in. Hence, given:

class Spam(Food):
    def method(self):
        print("Spam spam spam!")
        super(Spam, self).method()

class PickledSpam(Spam):
    pass


obj = PickledSpam()
obj.method()

super is being called from a PickledSpam instance, self, but the call is 
defined in the Spam class. super needs to know about both of them.
        
Of course, this hinges on how one defines "called from". I think it is 
helpful to understand methods as being called from the instance doing the 
calling rather than the class where they are defined.


-- 
Steven



More information about the Python-list mailing list