replace the base class

Matimus mccredie at gmail.com
Wed May 30 18:25:34 EDT 2007


This is a rather simplistic example, but you may be able to use a
mixin class to achieve what you need. The idea is that you write a
class that overrides only what is needed to add the new functionality.
For you it would be Color.

[code]
class Graph(object):
    def _foo(self):
        print "Graph._foo"

class Color(object):
    def _foo(self):
        print "Color._foo"


class Circle(Graph):
    def bar(self):
        self._foo()

class ColorCircle(Color,Circle): # the order of parent classes is
important
    pass

if __name__ == "__main__":
    c1 = Circle()
    c2 = ColorCircle()

    c1.bar() #prints: Graph._foo
    c2.bar() #pritns: Color._foo
[/code]


You might not have to do anything already. Try this and see if it
works:

[code]

ColorCircle(ColorGraph,Circle):
    pass

[/code]

Note that you will probably have to create an __init__ method, and
explicitly call the __init__ methods for the base classes. If the
__init__ for Circle ends up calling the __init__ method from Graph,
you may have issues. That is why the Color mixin that I created
inherited from object not Graph. It helps to avoid clashing __init__
methods.

Matt




More information about the Python-list mailing list