Access to C functions from a python class using self ?
Thomas Heller
theller at python.net
Mon Dec 30 10:25:48 EST 2002
christophe grimault <christophe.grimault at novagrid.com> writes:
> Hi there,
>
> I've been looking to 'ext & emb python', swig, cxx, ... for quite some
> time now, and I still can't figure out how to do the kind of stuff
> explained below...
>
>
> I define a class in python, let's say this simple class, just for the
> purpose of discussion :
>
>
> class Vector:
> def __init__(self, x1, x2, x3):
> self.x1, self.x2, self.x3 = x1, x2, x3
>
> def sum(self):
> return self.x1 + self.x2 + self.x3
>
> Now, I find that sum is slow, and I want to speedup things by
> replacing sum by a call to C.
You may want to try out psyco to accelerate this code.
>
> Looking at the manual, I can write something like:
>
> static PyObject *sum( PyObject *self, PyObject *args )
>
> and the manual tells me how to access elements passed in args, but
> does not tell how to access elements (in fact attributes) of self. How
> can I retrieve self.x1, self.x2, and self.x3 as defined in the python
> class from *self passed to the C function ?
>
PyObject_GetAttrString(self, "x1") does what you want.
Thomas
More information about the Python-list
mailing list