What is proper way to require a method to be overridden?

Thomas Ploch Thomas.Ploch at gmx.net
Thu Jan 4 22:40:34 EST 2007


jeremito schrieb:
> I am writing a class that is intended to be subclassed.  What is the
> proper way to indicate that a sub class must override a method?
> 
> Thanks,
> Jeremy
> 

What do you mean by 'indicate'? Writing it to the docstring of the
class/method? Writing a comment?

class Foo:
	"""
	When inheriting from Foo, method foo must be 	
	overridden. Otherwise SPAM.
	"""
	def foo(self):
		print 'bar'

class Bar(Foo):
	def __init__(self):
		Foo.__init__(self)
	
	# Has to be defined to override the base class's method
	# when inheriting from class Foo. Otherwise: SPAM
	def foo(self):
		print 'foo'

I don't know any other way.

Thomas



More information about the Python-list mailing list