By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,930 Members | 2,018 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,930 IT Pros & Developers. It's quick & easy.

replace the base class

P: n/a
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

May 30 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
May 30 '07 #2

P: n/a
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.

Expand|Select|Wrap|Line Numbers
  1. class Graph(object):
  2. def _foo(self):
  3. print "Graph._foo"
  4.  
  5. class Color(object):
  6. def _foo(self):
  7. print "Color._foo"
  8.  
  9.  
  10. class Circle(Graph):
  11. def bar(self):
  12. self._foo()
  13.  
  14. class ColorCircle(Color,Circle): # the order of parent classes is
  15. important
  16. pass
  17.  
  18. if __name__ == "__main__":
  19. c1 = Circle()
  20. c2 = ColorCircle()
  21.  
  22. c1.bar() #prints: Graph._foo
  23. c2.bar() #pritns: Color._foo
  24.  

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

Expand|Select|Wrap|Line Numbers
  1.  
  2. ColorCircle(ColorGraph,Circle):
  3. pass
  4.  
  5.  
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

May 30 '07 #3

P: n/a

Larry: Using "as", I cannot use both Circle and ColorCircle or more
like 3DCircle ... at the same time, only one of them.
But this is a easy solution in some cases.
On 31 mai, 00:25, Matimus <mccre...@gmail.comwrote:

Hi Martimus, your idea was a good one, I used it to meet my
_particular_ needs.
I was expecting something more complicate with metaclass, object
introspection ....

I used an interface (like in Java, a class with just method that must
work in //
with anoter class).
Finally the "replacement" of the base class in done with a simple :

class ColorCircle(ColorGraphInterface, Circle):
graph_base_class=Circle
Here is my full working test code

class Graph:
def plot(self):
print 'Graph.plot'

class Circle(Graph):
def draw(self):
print 'Circle.draw'
self.plot()

class Square(Graph):
def draw(self):
print 'Square.draw'
self.plot()

class ColorGraphInterface:
graph_base_class=None
def plot(self):
print 'ColorGraphInterface.plot'
self.graph_base_class.plot(self)
def draw(self):
print 'ColorGraphInterface.draw'
self.graph_base_class.draw(self)

class ColorCircle(ColorGraphInterface, Circle):
graph_base_class=Circle

class ColorCircle(ColorGraphInterface, Square):
graph_base_class=Square
cc=ColorCircle()
cc.draw()

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.

Expand|Select|Wrap|Line Numbers
  1. class Graph(object):
  2.     def _foo(self):
  3.         print "Graph._foo"
  4. class Color(object):
  5.     def _foo(self):
  6.         print "Color._foo"
  7. class Circle(Graph):
  8.     def bar(self):
  9.         self._foo()
  10. class ColorCircle(Color,Circle): # the order of parent classes is
  11. important
  12.     pass
  13. if __name__ == "__main__":
  14.     c1 = Circle()
  15.     c2 = ColorCircle()
  16.     c1.bar() #prints: Graph._foo
  17.     c2.bar() #pritns: Color._foo
  18.  

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

Expand|Select|Wrap|Line Numbers
  1. ColorCircle(ColorGraph,Circle):
  2.     pass
  3.  

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.
Yes I know, super() do a great job for that :-)
>
Matt

May 31 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.