C API - Creating Python Classes
Andrew Csillag
drew.csillag at starmedia.net
Wed Apr 26 14:33:51 EDT 2000
Mark Pope wrote:
>
> Hi,
>
> Using the python C API I can, for example, create an Integer Python object
> with the call:
>
> PyObject *pMyInt = PyInt_FromLong( 123 );
>
> I have a python class which I need to create an instance of. How is this
> done?
>
> I assume I need something like:
>
> PyObject *pModule = PyImport_ImportModule( "MyClassScript" );
>
> PyObject *pInstance = NEW_INSTANCE( pModule, args );
>
Close, but not quite...
PyObject *pModule = PyImport_ImportModule( "MyClassScript" );
PyObject *classObj = PyObject_GetAttrString( pModule, "MyClassName" );
PyObject *args = Py_BuildValue("(<argument tuple)", arg1, arg2, arg3
...);
PyObject *instance = PyObject_CallObject( classObj, args );
Obviously, you'd need to make <argument tuple> correspond to the
arguments to the MyClassName constructor. Not to mention the
appropriate Py_DECREF calls when you're done.
Cheers,
Drew
--
print(lambda(q,p):'pmt:$%0.2f'%(q*p/(p-1)))((lambda(a,r,n),t:(a*r/
t,pow(1+r/t,n*12)))(map(input,('amt:','%rate:','years:')),1200.0))
More information about the Python-list
mailing list