473,320 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

replace the base class

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
3 1970
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter Chatterton | last post by:
I have a class extending Vector, but I want it to extend another base class that contains variables. Can I implement some interfaces to do the same thing? The following from the Vector API...
8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
9
by: Jiho Han | last post by:
Suppose I have an xml fragment like: <mother> <child name="Bob" sex="M"/> <child name="Jane" sex="F"/> ... </mother> If I wanted to replace the <mother> element to <father> element, what is...
7
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
7
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
5
by: Dennis Jones | last post by:
Hello, I have a couple of classes that look something like this: class RecordBase { }; class RecordDerived : public RecordBase {
1
by: Dinis Correia | last post by:
Hi all, How can I replace an inherited member name? I've built a class that inherits from KeyedCollection(Of ..., ...). I would like to replace base class Items property with Fields property. Is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.