Returning an element from a C struct

Brad Gaspard bradley.s.gaspard at saic.com
Tue Mar 19 12:17:31 EST 2002


I'm trying to wrap a C structure in a Python class definition and have
come upon this problem.

class foo:

    __init__(self, dat):
        self.ptr = gnum_t(dat)  # initializes the C struct and returns a
pointer

    __getattr__(self,name):
        try:
            return self.__dict__[name]
        except:
            if name == 'A':
                return self.__dict__['ptr'].A
            elif name == 'B':
                return self.__dict__['ptr'].B
            elif name == 'C':
                return self.__dict__['ptr'].C
            # And many others

}

I know there has to be an easier way to do this??

Thanks in advance.

Brad




More information about the Python-list mailing list