Variable inheritance

Roman Suzi rnd at onego.ru
Mon May 21 23:45:41 EDT 2001


On Mon, 21 May 2001, Clarence Gardner wrote:

Hello!

Multiple inheritance says it bad design.
Probably, you want a third container class, which
will hold your Printer and OutputDevice classes
and provide communication between them.

>I had a particular class which inherited from two base classes, and
>I wanted to change it so there would be two flavors of it, which
>differed only in one of the base classes.
>
>Initially, it looked like this:
>
>class PSToLP(Printer.PSPrinter, OutputDevice.ToLP):
>    def __init__(self, PrinterName):
>        OutputDevice.ToLP.__init__(self, PrinterName)
>        Printer.PSPrinter.__init__(self)
>        self.PagesThisJob = 0
>    def PrintPage(self):
>        # We need to keep track of pages printed so we can start a new
>        # job every once in a while, so we don't hit size limits
>        Printer.PSPrinter.PrintPage(self)
>        self.PagesThisJob = self.PagesThisJob + 1
>        if self.PagesThisJob == self.MaxLPPages:
>            self.StartNewLPJob()
>            self.PagesThisJob =
>
>Now I want to make two similar classes like this:
>    PSToLP = GraphicLanguageToLP(Printer.PSPrinter)
>    PCLToLP = GraphicLanguageToLP(Printer.PCLPrinter)
>
>so I defined:
>
>def GraphicLanguageToLP(PrinterClass):
>    class ToLP(OutputDevice.ToLP):
>        def __init__(self, PrinterName):
>            OutputDevice.ToLP.__init__(self, PrinterName)
>            self.PClass.__init__(self)
>            self.PagesThisJob = 0
>        def PrintPage(self):
>            # We need to keep track of pages printed so we can start a new
>            # job every once in a while, so we don't hit size limits
>            self.PClass.PrintPage(self)
>            self.PagesThisJob = self.PagesThisJob + 1
>            if self.PagesThisJob == self.MaxLPPages:
>                self.StartNewLPJob()
>                self.PagesThisJob = 0
>    c = ToLP
>    c.__bases__ = (PrinterClass,) + c.__bases__
>    c.PClass = PrinterClass
>    return c
>
>I'm not Dutch, so I'm not sure if this is the obvious way to do it.
>(I'm running this under 1.5.2, by the way.)
>It seems like there may be something better, but I don't see it.
>
>Am I missing anything?
>
>Thanks
>
>

Sincerely yours, Roman Suzi
-- 
_/ Russia _/ Karelia _/ Petrozavodsk _/ rnd at onego.ru _/
_/ Tuesday, May 22, 2001 _/ Powered by Linux RedHat 6.2 _/
_/ "Never trust a computer you can't lift. - Stan Masor" _/





More information about the Python-list mailing list