Extend/Embed Python Method table Dispatcher

Paul Prescod paul at prescod.net
Sun Jul 23 02:35:20 EDT 2000


heath7952 at my-deja.com wrote:
> 
> Dear All,
> 
> I am extending Python with my own C functions. I would like be able to
> share one C function amongst different extended commands. Here's a
> little pseudo example.

How about this:

static PyMethodDef my_methods[] = {
   {"do_stuff",     (PyCFunction)Python_Embed::fnDoStuff1, METH_VARARGS,
dostuff__doc__   },
  {"do_stuff2",    (PyCFunction)Python_Embed::fnDoStuff2, METH_VARARGS,
dostuff2__doc__   },
  {NULL,           NULL}
};
 
PyObject* fnDoStuff(int what, PyObject* self, PyObject* args)
{
 
     switch(who called me)
        case do_stuff:
           do something
        break;
 
        case do_stuff2
           do something different.
        break;
 
}

PyObject* fnDoStuff1(PyObject* self, PyObject* args)
{
   fnDoStuff( 1, self, args )
}

PyObject* fnDoStuff2(PyObject* self, PyObject* args)
{
   fnDoStuff( 2, self, args )
}

-- 
 Paul Prescod - Not encumbered by corporate consensus
"Hardly anything more unwelcome can befall a scientific writer than 
having the foundations of his edifice shaken after the work is 
finished.  I have been placed in this position by a letter from 
Mr. Bertrand Russell..." 
 - Frege, Appendix of Basic Laws of Arithmetic (of Russell's Paradox)




More information about the Python-list mailing list