Question about subclassing - version 2

Maric Michaud maric at aristote.info
Fri Sep 8 07:15:12 EDT 2006


Le vendredi 08 septembre 2006 10:15, Bruno Desthuilliers a écrit :
> You
> mentioned NotImplementedError, which is indeed the usual way to make
> something "abstract" in Python.

Hummm, some more thoughts about this.

I can imagine class hierarchies where the presence of not implemented methods 
doesn't mean that the class is actually an abstract one. Even if partial 
implementation is not a common scheme it can save you from writing a lot of 
classes.

For example :

class car : #abstract
	def accelerate() : raise NotimplementedError
	def getBaseBuildPrice() : raise NotimplementedError
	def getOptionsPrice() : raise NotimplementedError
	def getPublicPrice() : raise NotimplementedError

class SerieA(car) :
	"""abstract, it's before the car
	get out of the factory"""
	def accelerate() : ...
	def getBaseBuildPrice() : ...

class CrashTestVendorSerieA(SerieA) :
	"""concrete, but doesn't
	implement getPublicPrice"""
	def getOptionsPrice() : ...

class CommercialSerieA(SerieA) :
	def getOptionsPrice() : ...
	def getPublicPrice() : ....

Doing the same with more traditional object design give :

class car : #abstract
	def accelerate() : raise NotimplementedError
	def getBaseBuildPrice() : raise NotimplementedError

class SerialCar : #abstract
	def getOptionsPrice() : raise NotimplementedError

class ComercialCar : #abstract
	def getPublicPrice() : raise NotimplementedError

class SerieA(car, SeriialCar) : #abstract
	def accelerate() : ...
	def getBaseBuildPrice() : ...

class CrashTestVendorSerieA(SerieA) : # concrete
	def getOptionsPrice() : ...

class CommercialSerieA(SerieA, CommercialCar) : # concrete
	def getOptionsPrice() : ...
	def getPublicPrice() : ...

And this can become a true spider net for more complicated cases. Obviously, 
in practice we will choose alternatives to inheritance (strategies, 
visitors, ...) to work with such complex situations, but it seems to me that 
partial implementation is not a bad choice, specifically in regard to duck 
typing.


-- 
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097



More information about the Python-list mailing list