Let’s say I have some code that creates a class:
class Item:
def __init__(self, param1):
do something with param1
now let’s say I have an object, ItemChild, that inherits from that class and inherits its initialization function, i.e., has some code that calls Item’s __init__ function. However, let’s say I also want it to do more in its __init__ function, and I want it to work with more parameters, i.e., param1, param2, param3, and param4. It seems like the call to the parent’s __init__ function will now be confused, since it won’t know which parameter is param1 when I pass params 1-3 into an instance of ItemChild. Is there any way to get around this, or is inheritance of the initialization method limited to that method and cannot be extended?
Here is the proper way to handle a scenario like this:
Note that
Iteminheriting fromobjectis important becausesuper()only works with new-style classes.As noted in comments, with 3.x you do not need to include the arguments to
super(), you can just usesuper().__init__(param1).