[Python-de] kleines Embedding Problem
Keule
m_konermann at gmx.de
Fri Apr 12 17:07:10 EDT 2002
Hallo zusammen !
Ich habe folgendes Beispiel in dem "Programming Python" Buch (zweite
Ausgabe, S. 1167) gefunden:
file module.py:
# call this class from C to make objects
class klass:
def method(self, x, y):
return "brave %s %s" % (x, y) # run me from C
und nun soll diese Python Funktion in C verwendet werden mit Hilfe des
folgenden Quellcodes:
file objects-low.c:
#include <Python.h>
#include <stdio.h>
main() {
/* run objects with low-level calls */
char *arg1="sir", *arg2="robin", *cstr;
PyObject *pmod, *pclass, *pargs, *pinst, *pmeth, *pres;
/* instance = module.klass() */
Py_Initialize();
pmod = PyImport_ImportModule("module"); /* fetch module */
pclass = PyObject_GetAttrString(pmod, "klass"); /* fetch module.class */
Py_DECREF(pmod);
pargs = Py_BuildValue("()");
pinst = PyEval_CallObject(pclass, pargs); /* call class() */
Py_DECREF(pclass);
Py_DECREF(pargs);
/* result = instance.method(x,y) */
pmeth = PyObject_GetAttrString(pinst, "method"); /* fetch bound method */
Py_DECREF(pinst);
pargs = Py_BuildValue("(ss)", arg1, arg2); /* convert to Python */
pres = PyEval_CallObject(pmeth, pargs); /* call method(x,y) */
Py_DECREF(pmeth);
Py_DECREF(pargs);
PyArg_Parse(pres, "s", &cstr); /* convert to C */
printf("%s\n", cstr);
Py_DECREF(pres);
}
In diesem Beispiel ist diese Klasseninstanz gebildet worden:
object = module.klass()
jetzt meine Frage:
was würde sich an dem obigen C Code ändern, wenn ich eine Klasseninstanz
mit Paramterübergabe entwerfen möchte, wie z.B. die folgende:
object = module.klass(x,y,z)
Hat jemand eine Idee für mich ?
Vielen Dank und schöne Grüsse
Marcus
More information about the Python-de
mailing list