471,330 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Python class method as an argument of a function in a C extension

Hi all,

I am trying to wrap some C code using SWIG (win32, mingw). I am new to
SWIG and to the Python/C API so what I am doing is looking at the
examples and trying to fit them to my needs, but I cannot find any
complete demo example of a C function taking as an argument a Python
function defined by the user.

What I am trying to do is to pass a class method as an argument of a
function defined in a C extension:

# file: runme.py

import myCExtensionModule

class MyClass:
def __init__(self):
self.myCounter = 0
self.myVar = 0
def myMethod(self, myArg):
self.myCounter += 1
return self.myVar + myArg
def runMe(self):
myCExtensionModule.aCFunction(self.myMethod, 10)

x = MyClass()

# end of runme.py

Can anybody give me an hint (or some link) on how to define
'aCFunction' and how to call 'self.myMethod' in the C source code?

Thank you very much for any kind of help!

Sep 26 '07 #1
0 922

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

54 posts views Thread by Brandon J. Van Every | last post: by
14 posts views Thread by David MacQuigg | last post: by
3 posts views Thread by Jim Newton | last post: by
10 posts views Thread by Andrew Dalke | last post: by
3 posts views Thread by Redefined Horizons | last post: by
reply views Thread by rosydwin | last post: by

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.