replace the base class

Larry Bates larry.bates at websafe.com
Wed May 30 17:05:24 EDT 2007


aspineux wrote:
> Hi
> 
> I would like a kind of function able to replace the base class like
> that:
> 
> class Graph:
>     pass
> 
> class Circle(Graph):
>     pass
> 
> class Square(Graph):
>     pass
> 
> class ColorGraph:
>     pass
> 
> def Adopt(new_base_class, child_class, old_base_class):
>     ....
>     return newclass
> 
> ColorCircle=Adopt(ColorGraph, Circle, Graph)
> ColorSquare=Adopt(ColorGraph, Square, Graph)
> 
> 
> I have a lot of classes (Circle, Square, ...) that inherit all from
> base class Graph
> I have a more powerful class ColorGraph that do the same as Graph and
> more.
> I want to have new classes ColorCircle, ColorSquare that share exactly
> the same code has
> Circle or Square but inherit from base class ColorGraph to take
> benefit the new features ?
> 
> How can I do that ?
> 
> I can get what I want by duplicating the source of all my child
> classes,
> and replace any occurrence of Graph by ColorGraph.
> But when the code of Circle or Square is changed, I don't want to redo
> the job.
> I could also have a lot of new base class : 3DGraph, ......
> 
> Thanks
> 
> Alain
> 
I believe what you are looking for is the 'as' clause on import

from module import Graph as Graph

class Circle(Graph):
    pass

Circle will have Graph as its baseclass


from module import ColorGraph as Graph

class Circle(Graph):
    pass

Circle will have ColorGraph as its baseclass

-Larry



More information about the Python-list mailing list