Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #67775

Re: How to create an instance of a python class from C++

References <0ab424e9-3a3f-4111-9f41-ff50ce73d70d@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2014-03-04 18:55 -0700
Subject Re: How to create an instance of a python class from C++
Newsgroups comp.lang.python
Message-ID <mailman.7780.1393984546.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Mar 4, 2014 at 5:14 PM, Bill <galaxyblue63@gmail.com> wrote:
> Hello:
>
> I can't figure out how to create an instance
> of a python class from 'C++':
>
> ( I am relatively new to Python so excuse some of
>   the following. )
>
> In a .py file I create an ABC and then specialize it:

Why are you creating an ABC?  Most Python classes do not use them.
Maybe you have a reason for it, but it's irrelevant to what you're
currently trying to do.

> Then from 'C++' (my implementation of RegisterClass)
> I try to create an instance
>
>     static PyObject *
>     RegisterClass( PyObject *, PyObject *args ) {       // This gets called ok.
>
>         PyObject *class_decl;
>         if( ! PyArg_ParseTuple(args, "O", &class_decl) )
>             return NULL;
>         Py_INCREF(class_decl);

So far, so good.  The object that was passed in was the "Derived"
class object.  Since you presumably only want class objects to be
passed in, you might want to check that here using PyType_Check.

>         PyTypeObject *typ = class_decl->ob_type;

Okay, now if class_decl is the class object that was passed in, then
class_decl->ob_type is the *type* of that class object -- the
metaclass, which in this case would be ABCMeta.  You probably don't
need this, because you want to instantiate Derived, not ABCMeta.

>         // Tried this.
>         // PyObject *an = _PyObject_New(class_decl->ob_type); assert(an);
>         // PyObject *desc = PyObject_CallMethod(an,"description",NULL); assert(desc);

In Python, you instantiate a class by calling it.  You should do the
same in C, using PyObject_CallFunction.  But as above, note that you
want to call class_decl, not class_decl->ob_type.

PyObject_New doesn't do any initialization and is, I believe, meant to
be used when implementing types in C.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to create an instance of a python class from C++ Bill <galaxyblue63@gmail.com> - 2014-03-04 16:14 -0800
  Re: How to create an instance of a python class from C++ Ian Kelly <ian.g.kelly@gmail.com> - 2014-03-04 18:55 -0700
    Re: How to create an instance of a python class from C++ Bill <galaxyblue63@gmail.com> - 2014-03-05 05:42 -0800
    Re: How to create an instance of a python class from C++ Grant Edwards <invalid@invalid.invalid> - 2014-03-05 16:08 +0000
      Re: How to create an instance of a python class from C++ Alister <alister.ware@ntlworld.com> - 2014-03-05 17:00 +0000
        Re: How to create an instance of a python class from C++ Grant Edwards <invalid@invalid.invalid> - 2014-03-05 17:14 +0000
          Re: How to create an instance of a python class from C++ Gene Heskett <gheskett@wdtv.com> - 2014-03-05 17:21 -0500
  Re: How to create an instance of a python class from C++ Barry Scott <barry@barrys-emacs.org> - 2014-03-11 21:37 +0000
  Re: How to create an instance of a python class from C++ Stefan Behnel <stefan_ml@behnel.de> - 2014-03-12 18:10 +0100

csiph-web