Access to C functions from a python class using self ?

Robin Munn rmunn at pobox.com
Mon Dec 30 16:32:57 EST 2002


christophe grimault <christophe.grimault at novagrid.com> wrote:
> 
> --------------080203000301060104010700
> Content-Type: text/plain; charset=us-ascii; format=flowed
> Content-Transfer-Encoding: 7bit
> 
> To be more precise, the class Vector given in original posting is just 
> here to explain the problem. In fact, the classes I use are signal 
> processing classes, for example, a class 'SpectrumAnalyser' that has a 
> method 'cross_periodogram' between to signal inputs is, for example, 
> much closer to my real needs.
> 
> Such a class maintains state attritutes and arrays of preceding 
> computations. They are part of *self in the C call, and I need to access 
> them.
> 
> Previous answers, from thomas and giorgi, talking about
> 
> 	PyObject_GetAttrString(self, "x1")
> 
> might be the kind of stuff I'm looking for. Are there any examples 
> (source codes) using this, which i could analyse in order to figure out 
> how this works with more complicated cases (tuples, Numpy arrays, ...)
> 
> Thanks in advance, to all
> 

Have you looked at the Python/C API reference?

    http://www.python.org/doc/current/api/api.html

If you haven't, then you're missing *the* best source of information on
how to do what you're trying to do.

If you've already looked there, then I apologize for pointing out the
obvious -- but I still recommend you take a second look, since most of
the answers you want can be found there. For example, clicking on the
"7.3.4: Tuple Objects" link takes you to:

    http://www.python.org/doc/current/api/tupleObjects.html

which details all the ways of manipulating Python tuples from C. The
other place you want to look regarding tuples is:

    http://www.python.org/doc/current/api/sequence.html

which contains the API that all sequence objects will respond to.

As for objects like Numpy arrays, if they're implemented as instances of
Python classes, then they should also be accessible as described in:

    http://www.python.org/doc/current/api/object.html

which is where you find, for example, PyObject_GetAttrString.

Also, the Modules directory in the Python source has a whole lot of code
you can look at to get an idea of how to use these functions.

I hope this helps you understand Python/C details a bit better.

-- 
Robin Munn <rmunn at pobox.com>
http://www.rmunn.com/
PGP key ID: 0x6AFB6838    50FF 2478 CFFB 081A 8338  54F7 845D ACFD 6AFB 6838



More information about the Python-list mailing list