Extending Python with MyClass
<p>I've read the tutorials on how to extend python with my own module with my own functions:</p>

<p><a href="http://docs.python.org/release/3.1.3/extending/embedding.html#embedding-python-in-c" rel="nofollow">http://docs.python.org/release/3.1.3/extending/embedding.html#embedding-python-in-c</a></p>

<p>But how do I extend python so that my module contains a class which I can use in python? A class I have programmed in C++.</p>

<p>I've tried using boost::python earlier but bjam hangs on me when I try to build on mac os x. I'd like to keep things simple as my requirement is very simple:</p>

<p>I have three functions in my module which I call initialise(), run() and close(). </p>

<p>At the moment I do this in python:</p>

<pre><code>import mymodule
mymodule.initialise()
mymodule.run()
mymodule.run() # run again
mymodule.close()
</code></pre>

<p>I'd like to have a class with initialise() as the constructor, run() as my method, and close() as my destructor. I can then do this:</p>

<pre><code>import mymodule
with mymodule.MyClass as my_class:
    my_class.run()
    my_class.run()
</code></pre>

<p>Here's some of my code at the moment:</p>

<pre><code>static PyMethodDef MyModuleMethods[] = 
{
    {"initialise",  mymodule_initialise, METH_VARARGS, ""},
    {"run",  mymodule_run, METH_VARARGS, ""},
    {"close",  mymodule_close, METH_VARARGS, ""},
    {NULL, NULL, 0, NULL}        /* Sentinel */
};
</code></pre>

<p>Thanks,</p>

<p>Barry</p>
