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

mauro mauro.tiz at gmail.com
Wed Sep 26 04:16:02 EDT 2007


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()
x.runMe()

# 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!




More information about the Python-list mailing list