What is super function in Python ?

Answer : A super() indicates the immediate parent of actual class. Just like `self` which is not a keyword, but to access parent.

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 :
[pastacode lang=”markdown” manual=”class%20LoggingDict(dict)%3A%0A%20%20%20%20def%20__setitem__(self%2C%20key%2C%20value)%3A%0A%20%20%20%20%20%20%20%20logging.info(‘Settingto%20%25r’%20%25%20(key%2C%20value))%0A%20%20%20%20%20%20%20%20super().__setitem__(key%2C%20value)” message=”” highlight=”” provider=”manual”/]
    • 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.
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like