473,383 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

API class creation


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
Aug 4 '05 #1
3 1567
kman3048 schrieb:
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()

--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Tel +49-241-93878-0
E-mail 'cGV0ZXIubWFhc0BtcGx1c3IuZGU=\n'.decode('base64')
-------------------------------------------------------------------
Aug 4 '05 #2
kman3048 wrote:
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
Aug 4 '05 #3

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 Dittmar wrote:
kman3048 wrote:
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


Aug 5 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
1
by: andrew queisser | last post by:
I've been trying to dynamically create a class DevT that's derived from a generic base GenBase<T>. It doesn't seem to work. I'm attaching a code sample below that illustrates the problem. ...
4
by: tony | last post by:
Hello! If I want to prevent creation of object for a class. I can accomplish this by using different technique. One way of doing this is to define the class to be abstract even if all the...
25
by: David Sanders | last post by:
Hi, As part of a simulation program, I have several different model classes, ModelAA, ModelBB, etc., which are all derived from the class BasicModel by inheritance. model to use, for example...
5
by: Nick Maclaren | last post by:
Hmm. The extensions documentation describes how to add instance members to a class (PyMemberDef), but I want to add a class member. Yes, this is constant for all instances of the class. Any...
10
by: Joel | last post by:
Is it true that if we don't specify a default constructor for our class, then the C# compiler provides us with its own that zeroes (or assigns default values) to the data members? I wrote a...
1
by: Anonymous | last post by:
I have been trying to write a template class for a shared memory container, over the last few days - but I keep getting numerous compiler errors e.g: Error: syntax error : identifier...
0
by: Anonymous | last post by:
I have been trying to write a template class for a shared memory container, over the last few days - but I keep getting numerous compiler errors e.g: Error: syntax error : identifier...
8
by: Mayur H Chauhan | last post by:
All, For my knowledge, if I declare Class as follow, then it thows compilation error. Protected Class Book End Class Even same for...
3
by: Andrus | last post by:
Two assemblies both referenced by application contain same class (created by script): namespace Script { public class Class1 { } } Compiling application
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.