ScanSkill

super

This super() function returns a proxy object that delegates method calls to a parent or sibling class of type.

Syntax

super(type[, object-or-type])

Here,

  • type: Required.
  • object-or-type: Optional.

Note: super() only works for new-style classes.

  • Allows us to avoid using the base class name explicitly
  • Working with Multiple Inheritance

The super() builtin returns a proxy object, a substitute object that can call methods of the base class via delegation. This is called indirection (ability to reference base object with super())

Examples

We call the function of one class from the other class using super().functionName() instead of FirstClass.FirstFunction()

>>> class Func1(object):
...     def feedback(self):
...         print 'hello'
...
>>> class Func2(Func1):
...     def feedback(self):
...         super(Func2, self).feedback()
...         print 'world'
...
>>> f = Func1()
>>> f.feedback()
hello
>>> b = Func2()
>>> b.feedback()
hello
world
>>> class Vehicle(object):
...      def __init__(self, vehicleName):
...      print(vehicleName, 'is a means of transportation.')
...    
>>> class Bike(Vehicle):
...    def __init__(self):
...      print('Bike is most used vehicle.')
...      super().__init__('Bike')
    
>>> d1 = Bike()
Bike is most used vehicle.
Bike is a means of transportation.