Inheritance in Python
Steve D'Aprano
steve+python at pearwood.info
Mon Apr 24 09:30:36 EDT 2017
On Mon, 24 Apr 2017 10:10 pm, id23092 at gmail.com 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
running.
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):
super().__init__(newID)
self.pathID = self.path2 + "/id" + str(newID)
--
Steve
“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