[Pyrex] classes and callbacks

Lenard Lindstrom len-l at telus.net
Tue Oct 10 02:10:00 UTC 2006


On 10 Oct 2006 at 13:00, Greg Ewing wrote:

> Simon Burton wrote:
> 
> > It sounds like he has a c-library that wants a  (void (*)()) function pointer.
> 
> There's an example showing how to deal with
> that in the Demos/callback directory.
> 
No, I think "Test Drive" understands this. What is requested is some 
way to make a C functions a class attributes. That way a C callback 
function can be directly associated with the class that uses it. This 
would replace a globally declared dictionary that contains callback 
functions keyed by object instance. No reason is given as to why a 
callback naming scheme like <class name>_<funtion name> will not work 
instead. Maybe there is an inheritance issue?

A work-around is to make the callback function an instance field, 
then register the callback in an __init__ method.

cdef unsigned long ABC_cb_func(int a, int b):
    return a*b

cdef class Base:
    """Abstract base class"""
    cdef unsigned long (* mycb_func)(int a, int b)

class ABC(Base):
    def __init__(self):
        self.mycb_func = ABC_cb_func

Lenard Lindstrom
<len-l at telus.net>





More information about the Pyrex mailing list