explain this with example: You may have seen code that doesn’t use super(), but instead…
- User
explain this with example: You may have seen code that doesn’t use super(), but instead calls the method directly on the superclass, like this: class NotRecommended(OrderedDict): """This is a counter example!""" def setitem(self, key, value): OrderedDict.setitem(self, key, value) self.move_to_end(key) This alternative works in this particular case, but is not recommended for two rea‐ sons. First, it hardcodes the base class. The name OrderedDict appears in the class statement and also inside setitem. If in the future someone changes the class statement to change the base class or add another one, they may forget to update the body of setitem, introducing a bug. The super() Function | 4893 It is also possible to provide only the first argument, but this not useful and may soon be deprecated, with the blessing of Guido van Rossum who created super() in the first place. See the discussion at “Is it time to dep‐ recate unbound super methods?”. The second reason is that super implements logic to handle class hierarchies with multiple inheritance. We’ll come back to that in “Multiple Inheritance and Method Resolution Order” on page 494. To conclude this refresher about super, it is useful to review how we had to call it in Python 2, because the old signature with two argu‐ ments is revealing: class LastUpdatedOrderedDict(OrderedDict): """This code works in Python 2 and Python 3""" def setitem(self, key, value): super(LastUpdatedOrderedDict, self).setitem(key, value) self.move_to_end(key) Both arguments of super are now optional. The Python 3 bytecode compiler auto‐ matically provides them by inspecting the surrounding context when super() is invoked in a method. The arguments are: type The start of the search path for the superclass implementing the desired method. By default, it is the class that owns the method where the super() call appears. object_or_type The object (for instance method calls) or class (for class method calls) to be the receiver of the method call. By default, it is self if the super() call happens in an instance method. Whether you or the compiler provides those arguments, the super() call returns a dynamic proxy object that finds a method (such as setitem in the example) in a superclass of the type parameter, and binds it to the object_or_type, so that we don’t need to pass the receiver (self) explicitly when invoking the method. In Python 3, you can still explicitly provide the first and second arguments to super().3 But they are needed only in special cases, such as skipping over part of the MRO for testing or debugging, or for working around undesired behavior in a superclass.
- Assistant
Certainly! Let's break down the concept with examples to understand why using
super()is recommended over directly calling methods on the superclass in Python, especially when dealing with inheritance and method overriding.The Scenario
Suppose we have a subclass that inherits from a superclass and we want to override a method from the superclass while still calling the original implementation. There are two ways to do this:
- Directly calling the superclass method by referencing the superclass name.
- Using the
super()function to call the superclass method.
Let's explore both methods with examples.
Example 1: Directly Calling the Superclass Method
Here's an example where we directly call the superclass method by referencing its name:
from collections import OrderedDict class NotRecommended(OrderedDict): """This is a counter example!""" def __setitem__(self, key, value): OrderedDict.__setitem__(self, key, value) self.move_to_end(key)In this code:
- We have a class
NotRecommendedthat inherits fromOrderedDict. - We override the
__setitem__method. - Inside
__setitem__, we directly callOrderedDict.__setitem__(self, key, value)to use the superclass implementation.
Why This Is Not Recommended
-
Hardcoding the Superclass Name:
- Issue: The name
OrderedDictis hardcoded inside the__setitem__method. - Implication: If we later change the superclass or add multiple superclasses, the reference inside
__setitem__will not automatically update. This can introduce bugs if someone forgets to update the method.
- Issue: The name
-
Multiple Inheritance Issues:
- Issue: Directly referencing
OrderedDictbypasses the method resolution order (MRO) provided by Python's inheritance mechanism. - Implication: In cases of multiple inheritance, directly calling the superclass method may skip other superclass implementations that should be called.
- Issue: Directly referencing
Example 2: Using
super()to Call the Superclass MethodHere's how you can use
super():from collections import OrderedDict class Recommended(OrderedDict): """This is the recommended approach.""" def __setitem__(self, key, value): super().__setitem__(key, value) self.move_to_end(key)In this code:
- We have a class
Recommendedthat also inherits fromOrderedDict. - We override the
__setitem__method. - We use
super().__setitem__(key, value)to call the superclass method.
Benefits of Using
super()-
No Hardcoding of Superclass Name:
- Advantage: We don't need to mention
OrderedDictinside the method. - Implication: If we change the superclass in the class definition,
super()will automatically refer to the new superclass. This reduces the risk of bugs due to forgotten updates.
- Advantage: We don't need to mention
-
Proper Handling of Multiple Inheritance:
- Advantage:
super()respects the method resolution order (MRO) in Python. - Implication: In multiple inheritance scenarios,
super()ensures that all appropriate superclass methods are called in the correct order.
- Advantage:
Demonstrating the Problem with an Example
Let's illustrate the issue by modifying the base class.
Changing the Superclass
Suppose we change the superclass from
OrderedDicttodict:from collections import OrderedDict class NotRecommended(dict): """Changed to inherit from dict instead of OrderedDict.""" def __setitem__(self, key, value): # Still calling OrderedDict.__setitem__, which is incorrect now OrderedDict.__setitem__(self, key, value) self.move_to_end(key) # AttributeError: 'NotRecommended' object has no attribute 'move_to_end'- Issue: The class now inherits from
dict, but inside__setitem__, it's still callingOrderedDict.__setitem__. - Error: This will cause incorrect behavior or errors because
dictdoesn't have themove_to_endmethod, and we're calling the wrong superclass method.
Using
super()After Changing the SuperclassNow, if we use
super():class Recommended(dict): """Changed to inherit from dict instead of OrderedDict.""" def __setitem__(self, key, value): super().__setitem__(key, value) # self.move_to_end(key) # This will still raise an AttributeError- Behavior:
super().__setitem__(key, value)now correctly refers todict.__setitem__. - Error Handling: Although
dictdoesn't havemove_to_end, the call tosuper()didn't introduce a bug by referencing the wrong class.
Multiple Inheritance and Method Resolution Order (MRO)
Let's consider a scenario with multiple inheritance:
class LoggingMixin: def __setitem__(self, key, value): print(f"Setting {key} = {value}") super().__setitem__(key, value) class CustomDict(LoggingMixin, dict): pass- Explanation:
CustomDictinherits from bothLoggingMixinanddict.LoggingMixinalso overrides__setitem__and callssuper().__setitem__.
Using
super()in Multiple InheritanceWhen we use
super(), Python's MRO ensures that all__setitem__methods in the inheritance chain are called appropriately.d = CustomDict() d['key'] = 'value'Output:
Setting key = value- Behavior: The
__setitem__method ofLoggingMixinis called, which prints the message and then callssuper().__setitem__. - MRO Chain:
super()moves todict.__setitem__.
Directly Calling the Superclass Method in Multiple Inheritance
If we had called
dict.__setitem__directly:class LoggingMixin: def __setitem__(self, key, value): print(f"Setting {key} = {value}") dict.__setitem__(self, key, value)- Issue: Bypasses any other superclass methods in the MRO.
- Implication: If there are other classes in the inheritance chain that also override
__setitem__, their implementations will be skipped.
Understanding
super()with Explicit ArgumentsIn Python 2,
super()required explicit arguments:class LastUpdatedOrderedDict(OrderedDict): def __setitem__(self, key, value): super(LastUpdatedOrderedDict, self).__setitem__(key, value) self.move_to_end(key)- Arguments:
LastUpdatedOrderedDict: The class wheresuper()is called.self: The instance of the class.
How
super()Works- Dynamic Proxy:
super()returns a proxy object that delegates method calls to a parent or sibling class. - Automatic Arguments in Python 3: In Python 3, both arguments are optional because the compiler can infer them from the context.
- Method Binding: The method found via
super()is bound toself, so you don't need to passselfwhen calling it.
Key Takeaways
-
Avoid Hardcoding Superclass Names:
- Using
super()makes your code more maintainable and less error-prone when changes are made to the class hierarchy.
- Using
-
Leverage Method Resolution Order (MRO):
super()works seamlessly with Python's MRO, ensuring that methods in multiple inheritance are called in the correct order.
-
Write Forward-Compatible Code:
- By using
super(), your subclasses remain compatible with future changes to the superclass or the addition of mixins.
- By using
-
Simplify Code Maintenance:
- If the base class or the class hierarchy changes, you don't need to update the method calls inside your methods.
Conclusion
Always prefer using
super()when calling methods from a superclass. It leads to cleaner, more maintainable, and more robust code, especially in complex inheritance scenarios.
Additional Example: Using
super()with Multiple InheritanceLet's demonstrate a complete example with multiple inheritance.
class A: def action(self): print("Action in A") class B(A): def action(self): print("Action in B") super().action() class C(A): def action(self): print("Action in C") super().action() class D(B, C): def action(self): print("Action in D") super().action() d = D() d.action()Output:
Action in D Action in B Action in C Action in A- Explanation:
- The
super().action()calls in each class ensure that all superclass methods are called following the MRO. - If we had directly called a superclass method like
A.action(self), we would have skipped other classes in the MRO.
- The
By using
super(), you ensure that your code is maintainable, respects the inheritance hierarchy, and avoids potential bugs that can occur by directly referencing superclass methods.