Re: python object model

scharf@EMBL-Heidelberg.DE
Tue, 30 Aug 1994 20:31:09 +0100

> BTW, I'm currently working on a Python programming book, in my spare time.

It 'd be great if there would be a python book, so please continue!

> I'd like to include a case study on client/server programming, since it's
> becoming such a pervasive thing. It looks like your examples might work
> pretty well. Any chance of using your examples in a book? You'd get full
> credit, etc. No promises here (I'm not even positive the book will ever
> get done at all...), but I'm looking for contributions.

Shure, use it! But I think I will enhance and stabilize it. When I have a
better version I will send it to you.

BTW: I'm currently thinking of how to make C++ classes, that can have
subclasses written in python and in C++:

class Parent {
PythonChild* pc;
protected: // subclass interface:
void virtual _Method1() {printf("Parent._Method1\n");}
public:
void SetPythonChild(PythonChild*_pc) {pc=_pc;}

// could be automated...
void Method1()
{ if(pc && pc->HasMethod('_Method1') // does python overwrite it?
pc->Call('_Method1')
else
_Method1(); // lets call our own (or our childs)
}
void DoSoemthing()
{
...
Method1(); // always call Method1 never _Method1
...
}
};

class Child : public Parent {
protected:
void _Method1(){printf("Child._Method1\n");}
};

#######
import CPP # for C++ :-)

class PyParent:
def __init__(self,parent):
parent.SetPythonChild(self)
def __getattr__(self,name):
return self.parent.getattr(name)

class PyChild(PyParent):
def _Method1(self):
print "PyChild._Method1"

class PyChild2(PyParent):
def _Method1(self):
parent._Method1() #call the parents method...
print "PyChild2._Method1"

p=PyParent(CPP.Parent())
p.DoSomething()
Parent._Method1

p=PyParent(CPP.Child())
p.DoSomething()
Child._Method1

p=PyChild(CPP.Parent())
p.DoSomething()
PyChild._Method1

p=PyChild2(CPP.Parent())
p.DoSomething()
Parent._Method1
PyChild2._Method1

p=PyChild2(CPP.Child())
p.DoSomething()
Child._Method1
PyChild2._Method1

I hope the principle gets clear from the code... I'm not good in describing
thinks :-(

Michael