Re: Extending with new Object types

Mark Hammond (mjh@cmutual.com.au)
Thu, 18 Aug 1994 23:47:33 GMT

>From article <DEREK.94Aug17095840@candyland.gamekeeper.bellcore.com>, by derek@candyland.gamekeeper.bellcore.com (25381-fields):
> I wrote a note before, but got no reply. I really need some help figuring
> out how to extend Python to add new objects.
>
> My example is that I want to create a Database class such that each
> object can be called with the methods of the class. The underlying
> implementation is in C++.
>
> I have read the documentation and looked at the xxobject.c file as well
> as numerous other source files, but am at a loss as to how to connect
> Python code with a call to my newDbobject function. I understand how
> all the other methods are connected to the class. What I don't understand
>
Yes - you _do_ need to create a new module in C.

Thr basic idea is that you add methods to the new module to
_create_ your new data type. Creating the instance itself is done via
the NEWOBJ macro. You will need a "Type" object for _both_ the module
and the new data type.

So the main module creates and returns a new instance of your data type.
Conceivable, this may be the only method in your module.

Then, you create methods for your data type. Once an object has been created
and returned to Python, you can use its methods.

Sample Python code may look like:
newobj = yourmodule.CreateObject()
newobj.SomeMethod()

Ther best sample code for this sort of stuff (specifically the object methods)
is probably inside some of the core modules - look maybe at socketmodule.

{I think the above is correct - that is what I do anyway. Hopefully someone will pipe up with a better answer if one exists}

HTH...

Mark.