Inheritance in Python

Steve D'Aprano steve+python at
Mon Apr 24 09:30:36 EDT 2017

On Mon, 24 Apr 2017 10:10 pm, id23092 at wrote:

> I do not quite understand how inheritance works in Python.

> class mainCL():
>      def __init__(self):
>          self.path1 = "/a"
>          self.path2 = "/b"
> class secondCL(mainCL):
>      def __init__(newID):
>          self.pathID = self.path2+"/id"+str(newID)

secondCL over-rides the __init__ method of mainCL and prevents it from

If you want to overload a method, you have to ensure that the superclass
method is called. Do this instead:

class secondCL(mainCL):
    def __init__(self, newID):
        self.pathID = self.path2 + "/id" + str(newID)

“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

More information about the Python-list mailing list