473,473 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Implementing class methods in C


I am having a problem implementing some methods of a python class in C.
The class is defined in python, but I would like to rewrite some methods
in c. Here is an example of what I want to do:

file _test.c:

#include <Python.h>

static PyObject *
func2(PyObject *self, PyObject *args)
{
if (self == NULL) {
PyErr_SetString(PyExc_SystemError, "self is NULL");
return NULL;
}

// Parse arguments
if (!PyArg_ParseTuple(args, ""))
{
return NULL;
}

Py_INCREF(Py_None);
return Py_None;
}

static PyMethodDef TestMethods[] = {
{"func2", func2, METH_VARARGS, "func2."},
{NULL, NULL, 0, NULL} /* Sentinel */
};

PyMODINIT_FUNC
init_test(void)
{
(void) Py_InitModule("_test", TestMethods);
}

----------------------------------------------------
test.py:

class Test:
def func1(self):
print "I am in func 1"

import _test
import new
Test.func2 = new.instancemethod(_test.func2, None, Test)
del(new)

t = Test()
t.func2()
When I run test.py, I get a SystemError exception (which is what I raise
if self is NULL). I think my confusion lies in the use of PyObject* self
in the function declaration. Shouldn't this be set to point to the
instance of class Test that I am calling it from? Am I misunderstanding
the purpose of PyObject* self? Thanks.

Naveen

---------------------------------------------------------------------
Naveen Michaud-Agrawal
Program in Molecular Biophysics
Johns Hopkins University
(410) 614 4435
Aug 15 '05 #1
0 1447

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

Similar topics

1
by: Maurice | last post by:
Hi, We are implementing some wrappers in C++ according to the Adapter Pattern. The classes and their methods in the Adaptee classes (open-source library) have already the interface that we like,...
6
by: Martyn Lawson | last post by:
Hi, I am currently working as an Analyst on a .NET Web Project using ASP.NET and C#.NET. I have a couple of, at least what should be, quick questions: 1. My understanding of UML says that...
1
by: nmichaud | last post by:
I am having a problem implementing some methods of a python class in C. The class is defined in python, but I would like to rewrite some methods in c. Here is an example of what I want to do: ...
7
by: mdc | last post by:
Hi, Is there any way to implement an interface as a static method in C#? If not, is this a bug? Micheal.
4
by: John C | last post by:
I'm new to C#, so just point me at the correct reference material if this question has been answered before. When creating a new class which implements the IDictionary interface, two versions of...
6
by: Joseph Geretz | last post by:
I have the following class which I am serializing and passing back and forth between my Web Service application and the client. public class Token : SoapHeader { public string SID; public...
3
by: Daniel Kraft | last post by:
Hi, I usually program in C++, but for a special project I do have to code in C (because it may be ported to embedded-like platforms where no C++ compiler exists). I do want to realize some...
8
by: puzzlecracker | last post by:
0) What is the convention name for derived classes? 1) If we implement methods in abstract class, do we still need to declare them as abstract? 2) Are we allowed to override methods in derived...
21
by: puzzlecracker | last post by:
Problem: I send a lot of requests to the application (running on a different box, of course), and I receive back responses from the app . Below: socket corresponds to Socket socket=new...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.