Home > Enterprise >  Python3 - How does using `super` inside a method being overridden works (more in body)
Python3 - How does using `super` inside a method being overridden works (more in body)

Time:04-07

There is class structure like shown below:

class RegressionResults(LikelihoodModelResults):
    ...
    ...
    def conf_int(self, alpha=.05, cols=None):
        ci = super(RegressionResults, self).conf_int(alpha=alpha, cols=cols)
        return ci

and here is LikelihoodModelResults:

class LikelihoodModelResults(Results):
    ...
    ...
    def conf_int(self, alpha=.05, cols=None):
    ...
    ...

and the class Results doesn't have a method called conf_int.

Given such structure of classes and inheritence, I would like to know what happens when conf_int is called on the instance of RegressionResults. will the code inside conf_int of LikelihoodModelResults be run? if yes, why even try to override it?

I am sorry, if i have been vague in the question's title, you are free to edit it if you have a better title. And sources of above classes are RegressionResults, LikelihoodModelResults.

CodePudding user response:

In terms of code the overridden method does nothing extra, so you're right that the override seems redundant. However, in the original source it has a large docstring and then this comment inside the method body:

# keep method for docstring for now

So I assume the override is there for documentation purposes rather than mechanical ones.

  • Related