function override???

Adrian Eyre a.eyre at optichrome.com
Tue Nov 16 18:05:44 CET 1999


> ;---fprot.py
> def overrideme(self, str, someint)
>
> ;--fimp.py
> import fprot   #I know this ain't right.
>                           # I don't know how to import from other python
> scripts yet.  Enlighten me?
>
> def overrideme(self, str, someint)
>     do_some_junk()

Probably easier to use a couple of classes:

###
# interface.py
class Interface:
	def callback1(self, args):
		pass

###
# implementation.py
from interface import Interface

class Implementation(Interface):
	def callback1(self, args):
		do_some_junk()

###

Then in the C code do something like:

// N.B. This will leak a bit. Need to DECREF some stuff...
PyObject* mod = PyImport_ImportModule("implementation");
PyObject* dict = PyModule_GetDict(mod);
PyObject* imp_class = PyDict_GetItemString(dict, "Implementation");
PyObject* imp_inst = PyEval_CallObject(imp_class, PyTuple_New());
PyObject* callback1 = PyObject_GetAttrString(imp_inst, "callback1");
PyObject* result = PyEval_CallObject(callback1, Py_BuildValue("(s)",
"arg"));

--------------------------------------------
Adrian Eyre <mailto:a.eyre at optichrome.com>
Optichrome Computer Solutions Ltd
Maybury Road, Woking, Surrey, GU21 5HX, UK
Tel: +44 1483 740 233  Fax: +44 1483 760 644
http://www.optichrome.com
--------------------------------------------





More information about the Python-list mailing list