Variable inheritance

Clarence Gardner clarence at netlojix.com
Mon May 21 21:36:34 EDT 2001


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




More information about the Python-list mailing list