Skip to content Skip to sidebar Skip to footer

Python's Super(), Abstract Base Classes, And Notimplementederror

Abstract base classes can still be handy in Python. In writing an abstract base class where I want every subclass to have, say, a spam() method, I want to write something like this

Solution 1:

You can do this cleanly in python 2.6+ with the abc module:

import abc
classB(object):
    __metaclass__ = abc.ABCMeta
    @abc.abstractmethoddeffoo(self):
        print'In B'classC(B):
    deffoo(self):
        super(C, self).foo()
        print'In C'

C().foo()

The output will be

In B
In C

Solution 2:

Do not write all that code. Simple inspection of the abstract class can save you writing all that code.

If the method is abstract, the concrete subclass does not call super.

If the method is concrete, the concrete subclass does call super.

Solution 3:

The key point to understand this is super() is for implementing cooperative inheritance. How the classes cooperate is up to you the programmer. super() is not magic and does not know exactly what you want! There is not much point in using super for a flat hierarchy that doesn't need cooperative inheritance, so in that case S. Lott's suggestion is spot on. Subclasses of Useful may or may not want to use super() depending their goals :)

For example: Abstract is A. A <- B, but then you want to support insertion of C like so A <- C <- B.

classA(object):                                                                                         
    """I am an abstract abstraction :)"""deffoo(self):
        raise NotImplementedError('I need to be implemented!')

classB(A):
    """I want to implement A"""deffoo(self):
        print('B: foo')
        # MRO Stops here, unless super is not A
        position = self.__class__.__mro__.index
        ifnot position(B) + 1 == position(A):
            super().foo()

b = B()     
b.foo()

classC(A):
    """I want to modify B and all its siblings (see below)"""deffoo(self):
        print('C: foo')
        # MRO Stops here, unless super is not A
        position = self.__class__.__mro__.index
        ifnot position(C) + 1 == position(A):
            super().foo()

print('')   
print('B: Old __base__ and __mro__:\n')
print('Base:', B.__bases__)
print('MRO:', B.__mro__)
print('')
# __mro__ change implementation
B.__bases__ = (C,)
print('B: New __base__ and __mro__:\n')
print('Base:', B.__bases__)
print('MRO:', B.__mro__)
print('')
b.foo()

And the output:

B: foo

B: Old __base__ and __mro__:

Base: (<class'__main__.A'>,)MRO: (<class'__main__.B'>, <class '__main__.A'>, <class 'object'>)B:New __base__ and __mro__:

Base: (<class'__main__.C'>,)MRO: (<class'__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)B: foo
C: foo

Post a Comment for "Python's Super(), Abstract Base Classes, And Notimplementederror"