Super Function in Python

  • A super() indicates the immediate parent of actual class. Just like `self` which is not a keyword, but to access parent.
  • Before `super`, the method was to explicitly use the type of master as specifier and to add `self` on function signature.

In Python, super() built-in has two major use cases:

    • Allows us to avoid using base class explicitly
    • Working with Multiple Inheritance
Example :
class LoggingDict(dict):
def __setitem__(self, key, value):
logging.info('Settingto %r' % (key, value))
super().__setitem__(key, value)
    • These classes have all the same capabilities like as its parent, dict, but it extends the __setitem__ method to create log entries whenever a key is updated. After creating a log entry, method uses super() to delegate the work for essentially updating the dictionary with the key or value pair.
    • Before super() was presented, it have hardwired the call among dict.__setitem__(key, value and self). In any case, super() is improved since it is computed indirect reference.

Categorized in:

Tagged in:

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,