What is the difference between `super (...)` and `return super (...)`?

I am only now learning about POPON OOP. In some framework source code, I came across return super(... and wondered if there was a difference between the two.

 class a(object): def foo(self): print 'a' class b(object): def foo(self): print 'b' class A(a): def foo(self): super(A, self).foo() class B(b): def foo(self): return super(B, self).foo() >>> aie = A(); bee = B() >>> aie.foo(); bee.foo() a b 

Looks like me. I know that OOP can become quite complicated if you allow, but I don’t have the means to come up with a more complex example at this stage of my training. Is there a situation where returning super will be different than calling super ?

+7
python oop
source share
1 answer

Yes. Consider the case where, instead of typing, the superclass foo returned something:

 class BaseAdder(object): def add(self, a, b): return a + b class NonReturningAdder(BaseAdder): def add(self, a, b): super(NonReturningAdder, self).add(a, b) class ReturningAdder(BaseAdder): def add(self, a, b): return super(ReturningAdder, self).add(a, b) 

For two instances:

 >>> a = NonReturningAdder() >>> b = ReturningAdder() 

When we call foo on a , nothing seems to happen:

 >>> a.add(3, 5) 

When we call foo on b , we get the expected result:

 >>> b.add(3, 5) 8 

This is because when both NonReturningAdder and ReturningAdder call BaseAdder foo , NonReturningAdder discards the return value, while ReturningAdder passes it.

+12
source share

All Articles