Difference between super() and super (className,self) in Python

Viewed 2975

I have code snipped like :

class A:
    def test(self):
        return 'A'

class B(A):
    def test(self):
        return 'B->' + super(B, self).test()

print(B().test())

Output : B->A

If I write something like this then I'm getting the same output :

class A:
    def test(self):
        return 'A'

class B(A):
    def test(self):
        return 'B->' + super().test()  # change to super()


print(B().test())

In both cases I'm getting the same output. Then, I want to know what's the difference between this two types of calling of super? What are the pros and cons of using either of them?

1 Answers

In Python 2, only the super(className,self) syntax was possible. Since It was the most used version, as of Python 3 providing no arguments will act the same.

There are two typical use cases for super. In a class hierarchy with single inheritance, super can be used to refer to parent classes without naming them explicitly, thus making the code more maintainable

Related