4

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

Al Bundy
  • 707
  • 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

6

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
  • 289,723
  • 53
  • 439
  • 496
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
  • 242,874
  • 37
  • 412
  • 384