"implemented by subclass"
Erik Max Francis
max at alcyone.com
Sat Feb 16 18:35:08 EST 2002
More information about the Python-list mailing list
Sat Feb 16 18:35:08 EST 2002
- Previous message (by thread): "implemented by subclass"
- Next message (by thread): win32gui
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
phil hunt wrote: > Consider the following python code: > > class Log: > def write(self, text): > """(implemented by subclass)""" > > Is this the most pythonic way to do it? If not, what would be > better? How about rasing a ShouldBeImplementedBySublass exception? > > How do other people do it? I use raise NotImplementedError, and also typically put a test in the constructor: class Log: def __init__(self): if self.__class__ is Log: raise NotImplementedError ... def write(self, text): raise NotImplementedError The one drawback I know of here is that this can have a somewhat (naively) unexpected effect if you're using mutiple inheritance; in the case where you are deriving from to classes, one of which implements a method and the other does not (with the raise NotImplementedError stub above), it's best to have the new class (derived from both) explicitly invoke the right method in its parent class, otherwise the lookup mechanism may find the first method which has a definition, but which is in fact the stubbed-out NotImplementedError method as above! -- Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/ __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE / \ Laws are silent in time of war. \__/ Cicero Esperanto reference / http://www.alcyone.com/max/lang/esperanto/ An Esperanto reference for English speakers.
- Previous message (by thread): "implemented by subclass"
- Next message (by thread): win32gui
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list