473,385 Members | 1,983 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,385 software developers and data experts.

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 1444

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...
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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:
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.