Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Creating Python Class Instances From C API

703 views
Skip to first unread message

bill barrington

unread,
Oct 12, 1999, 3:00:00 AM10/12/99
to

I would like to create a python class instance using the python/C
API for eventual return back into "python space". IOW, I want to
accomplish the equivalent of the following python code, but from within
an extension:

instance = Class(<whatever>)

...and then return the newly created instance.

Anyone know how to do this?

--
Bill Barrington
CNN Interactive - http://cnn.com
bill.ba...@cnn.com
404 827-5572


Fredrik Lundh

unread,
Oct 12, 1999, 3:00:00 AM10/12/99
to bill barrington
bill barrington <bill.ba...@turner.com> wrote:
> I would like to create a python class instance using the python/C
> API for eventual return back into "python space". IOW, I want to
> accomplish the equivalent of the following python code, but from within
> an extension:
>
> instance = Class(<whatever>)

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

and then look at the cPickle.c source code
(see find_class, Instance_New, load_inst, ...).

</F>

coming soon:
http://www.pythonware.com/people/fredrik/librarybook.htm


bill barrington

unread,
Oct 12, 1999, 3:00:00 AM10/12/99
to Fredrik Lundh
Fredrick,

    Thanks for pointing me in the right direction.  The only bit of trouble I had was figuring out that I needed to set 'args' to NULL and use the keyword dictionary parameter instead in the call to PyInstance_New, i.e.

PyObject *data = Py_BuildValue("{s:s}", "<name>", "<value>");
PyObject *object = PyInstance_New(class_name, NULL, data);
    It works great!  Thanks again.

Regards,

Bill
 

-- 
Bill Barrington
CNN Interactive - http://cnn.com
bill.ba...@cnn.com
404 827-5572
 

Adrian Eyre

unread,
Oct 13, 1999, 3:00:00 AM10/13/99
to bill barrington
> PyObject *data = Py_BuildValue("{s:s}", "<name>", "<value>");
> PyObject *object = PyInstance_New(class_name, NULL, data);

If you check out the code in Modules/newmodule.c, you can even
create an instance of a class without calling its constructor.

This can be very handy for having an instance returned from
a different class, which can't be instantiated by the Python
programmer.

--------------------------------------------
Adrian Eyre <mailto:a.e...@optichrome.com>
Optichrome Computer Solutions Ltd
Maybury Road, Woking, Surrey, GU21 5HX, UK
Tel: +44 1483 740 233 Fax: +44 1483 760 644
http://www.optichrome.com
--------------------------------------------


M.-A. Lemburg

unread,
Oct 14, 1999, 3:00:00 AM10/14/99
to bill barrington, Python List @ Python.org
bill barrington wrote:
>
> I would like to create a python class instance using the python/C
> API for eventual return back into "python space". IOW, I want to
> accomplish the equivalent of the following python code, but from within
> an extension:
>
> instance = Class(<whatever>)
>
> ...and then return the newly created instance.
>
> Anyone know how to do this?

The trick is to get a reference to the class object and then
*call* it with your init parameters. It will then return an
instance just like in Python.

For code, see the examples Fredrik pointed you at.

Hope that helps,
--
Marc-Andre Lemburg
______________________________________________________________________
Y2000: 78 days left
Business: http://www.lemburg.com/
Python Pages: http://www.lemburg.com/python/

0 new messages