What is proper way to require a method to be overridden?
Carroll, Barry
Barry.Carroll at psc.com
Fri Jan 5 19:22:11 EST 2007
> -----Original Message-----
> From: python-list-bounces+barry.carroll=psc.com at python.org
[mailto:python-
> list-bounces+barry.carroll=psc.com at python.org] On Behalf Of Fuzzyman
> Sent: Friday, January 05, 2007 4:05 PM
> To: python-list at python.org
> Subject: Re: What is proper way to require a method to be overridden?
>
>
> Carl Banks wrote:
> > jeremito wrote:
> > > 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?
> >
> > You can't (easily).
> >
>
> Well...
>
> How about not defining it on the base class, but check in the
> constructor that the attribute exists and that it is of type
> FunctionType ?
>
> Fuzzyman
> http://www.voidspace.org.uk/python/articles.shtml
>
> --
> http://mail.python.org/mailman/listinfo/python-list
Greetings:
Thomas Christopher's book "Python Programming Patterns" has an example
of this that I like. On page 72 he shows code for a class, AbstractSet,
which he later subclasses in ListSet and DictSet. Each of the methods
intended for subclassing simply to raise a NotImplementedError, e.g.:
def insert(self,x): raise NotImplementedError, "set.insert"
In this way the superclass's interface is well defined (the methods and
their parameters are all listed, but if invoked before they are
overwritten, they abort with a useful error message. Pretty slick,
IMHO.
Regards,
Barry
barry.carroll at psc.com
541-302-1107
________________________
We who cut mere stones must always be envisioning cathedrals.
-Quarry worker's creed
More information about the Python-list
mailing list