-4

I sort of understand that the keyword super in the ChildClass that inherits from a BaseClass is to prevent the overriding of the the base's class method when a ChildClass has the same method. What is the practical use of this?

For example:

class BaseClass():
    def save(*args, **kwargs):
        print "save from BaseClass"

class ChildClass(BaseClass):
    def save(self, *args, **kwargs):
        super(ChildClass, self).save(*args, **kwargs)
        print "save from ChildClass"


someObject = ChildClass()
someObject.save()
​

Output:

save from BaseClass
save from ChildClass
cloudviz
  • 971
  • 4
  • 15
  • 40
  • 2
    `super()` doesn't call anything; you still have to call the parent method explicitly. It gives you access to the implementation of a method on a parent class, so you can *re-use* that implementation as needed. – Martijn Pieters Apr 02 '15 at 09:44
  • 1
    I'm not entirely clear what your question is here though. What if `ChildClass.save()` had to do some additional processing, but you don't want to replicate *all the work* `BaseClass.save()` does? That's where `super()` comes in, you can reuse the original `save()` method without having to do all that work again, but you can add to the original behaviour whilst presenting a consistent API to the users of `ChildClass`. – Martijn Pieters Apr 02 '15 at 09:45
  • I *think* what you are asking about is what the use is of calling the parent method, but you could be asking about something else entirely. – Martijn Pieters Apr 02 '15 at 09:46
  • 1
    Have you read e.g. http://stackoverflow.com/q/222877/3001761? http://stackoverflow.com/q/576169/3001761? – jonrsharpe Apr 02 '15 at 09:46
  • @jonrsharpe: thank you for illustrating my point; it could be that the OP is asking about using `super()` vs. naming the base class method directly. – Martijn Pieters Apr 02 '15 at 09:47
  • I've edited the question. I'm looking for practical use why would you do both somemethod() from BaseClass and somemethod() from ChildClass? – cloudviz Apr 02 '15 at 09:50
  • 1
    Imagine that `BaseClass.save()` can store some document, but does not inform an user about save status. So, you can `extend BaseClass` and call `super.save(...)` (to save the document) and than inform the user about the save status... Why do you call with `super` prefix? As your child method calls exactly as parent (`.save(...)`), you need to tell to compiler to call parent save. If you don't do that, it will call itself infinitely and throw an error... – Valijon Apr 02 '15 at 09:54

1 Answers1

3

I sort of understand that...

You have had two attempts at this, neither correct:

  • "will call both the parent's class method and the child's class method" - no, it calls the method on the next class in the MRO (Method Resolution Order); and
  • "is to prevent the overriding of the the base's class method when a ChildClass has the same method" - no, the child class implementation does override the base class, super allows you to access inherited implementations without explicitly specifying a class.

What is the practical use of this?

At the very simplest, imagine that we have a base class with one initialisation parameter:

def Base(object):

    def __init__(self, attr1):
        self.attr1 = attr1

and a child class with two. Then we can implement the child in one of two ways, either without calling the base class:

def Child(Base):

    def __init__(self, attr1, attr2):
        self.attr1 = attr1 
        self.attr2 = attr2

or by calling the base class:

def Child(Base):

    def __init__(self, attr1, attr2):
        super(Child, self).__init__(attr1)
        self.attr2 = attr2

Note that this reduces repetition, and if we change the handling of attr1 in the Base class (e.g. we assign the parameter to a "private" attribute _attr1 and access it via a @property) we only have to make that change once.


You could also implement the second as:

def __init__(self, attr1, attr2):
    Base.__init__(self, attr1)
    self.attr2 = attr2

but this again means repetition - we've now had to specify the name Base in two places in Child, so if we rename the Base or change the inheritance for Child we have to make alterations in two places.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Ok thanks for correcting me and for your explanations. In that case I don't completely understand it at all. That's why my question starts with I "sort of" – cloudviz Apr 02 '15 at 10:02