API class creation

K

kman3048

Hello,

as a relative newcomer to Python API programming I've got a problem:

To extend Python:
- there is an API C call to create a module
- there is also a API C call to create a method
- there is an API C call to create a Class instance

Now, I need to create a Class and fill it with Methods and Variables.
There are means to create (and attache) methods and variables.
However, I have not found how to create a Class within a Module. Or do
I have to use a low level API function to allocate an Object from Heap?

One possible solution I think is not very elegant:
- define a module and class within Python scripting
- use this object by creating Instances of this object via API

Is there no better way (however, I don't know if above works anyway).

Thanks,
Matt
 
P

Peter Maas

kman3048 said:
Now, I need to create a Class and fill it with Methods and Variables.
There are means to create (and attache) methods and variables.
However, I have not found how to create a Class within a Module. Or do

import aModule
c = aClassGenerator()
setattr(aModule,'c',c)
ci = aModule.c()
 
D

Daniel Dittmar

kman3048 said:
Hello,

as a relative newcomer to Python API programming I've got a problem:

To extend Python:
- there is an API C call to create a module
- there is also a API C call to create a method
- there is an API C call to create a Class instance

Now, I need to create a Class and fill it with Methods and Variables.
There are means to create (and attache) methods and variables.
However, I have not found how to create a Class within a Module. Or do
I have to use a low level API function to allocate an Object from Heap?

static PyMethodDef moduleMethods [] = {
...
};

statichere PyTypeObject MyClassType = {
PyObject_HEAD_INIT (NULL)
...
};

initmymodule ()
{
PyObject* module;
PyObject* dict;

module = Py_InitModule4 ("mymodule", moduleMethods,
"doc string", NULL, PYTHON_API_VERSION);
if (module == NULL) {
return;
}
dict = PyModule_GetDict (module);
PyDict_SetItemString (dict, "MyClass"),
(PyObject*) &MyClassType));
}

Daniel
 
K

kman3048

Thanks, that helped a lot.

Anybody who wants to attempt the same might find the
source code tree of Python i.e. file src/Modules/cdmodule.c
helpful.

--Matt


Daniel said:
kman3048 said:
Hello,

as a relative newcomer to Python API programming I've got a problem:

To extend Python:
- there is an API C call to create a module
- there is also a API C call to create a method
- there is an API C call to create a Class instance

Now, I need to create a Class and fill it with Methods and Variables.
There are means to create (and attache) methods and variables.
However, I have not found how to create a Class within a Module. Or do
I have to use a low level API function to allocate an Object from Heap?

static PyMethodDef moduleMethods [] = {
...
};

statichere PyTypeObject MyClassType = {
PyObject_HEAD_INIT (NULL)
...
};

initmymodule ()
{
PyObject* module;
PyObject* dict;

module = Py_InitModule4 ("mymodule", moduleMethods,
"doc string", NULL, PYTHON_API_VERSION);
if (module == NULL) {
return;
}
dict = PyModule_GetDict (module);
PyDict_SetItemString (dict, "MyClass"),
(PyObject*) &MyClassType));
}

Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top