pure virtual functions in python

Peter Hansen peter at engcorp.com
Sun Jan 12 15:59:14 CET 2003


polux wrote:
> 
> how to emulate them ?
> I mean, how to force a user to redifine some functions in a derivated
> class ?

>>> class PureVirtual:
...     def method(self):
...         raise NotImplementedError('PureVirtual.method must be supplied in child class')
...
>>> class BadChild(PureVirtual):
...     pass
...
>>> b = BadChild()
>>> b.method()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 3, in m
NotImplementedError: PureVirtual.method must be supplied in child class

-Peter




More information about the Python-list mailing list