3

How do you specify in a Python parent class that certain fields/methods need to be overridden in the child classes?

Al Bundy
  • 677
  • 2
  • 5
  • 12
  • Possible duplicate of http://stackoverflow.com/questions/1151212/equivalent-of-notimplementederror-for-fields-in-python – LSerni Feb 05 '13 at 09:46

2 Answers2

5

You could raise a NotImplementedError:

def my_method(self, arg):
    raise NotImplementedError('Implement me')

For properties, you can use the @property decorator:

@property
def my_property(self):
    raise NotImplementedError('Implement me as well')
Blender
  • 275,078
  • 51
  • 420
  • 480
1

You could look at the abstract base class module.

However, a simpler alternative is just to define a stub implementation that does nothing, or raises NotImplementedError.

BrenBarn
  • 228,001
  • 34
  • 392
  • 371