Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!newsreader4.netcologne.de!news.netcologne.de!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.022 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'python,': 0.02; 'static': 0.04; 'subject:create': 0.09; 'subject:instance': 0.09; 'subject:How': 0.10; 'python': 0.11; '*args': 0.16; '*type*': 0.16; '.py': 0.16; 'hello:': 0.16; 'subject:class': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'do.': 0.18; 'trying': 0.19; 'implementing': 0.19; 'skip:p 40': 0.19; 'meant': 0.20; 'creating': 0.23; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; '(my': 0.31; 'null;': 0.31; 'object.': 0.31; 'ok.': 0.31; 'file': 0.32; 'class': 0.32; 'this.': 0.32; 'probably': 0.32; 'figure': 0.32; 'maybe': 0.34; 'subject:from': 0.34; "can't": 0.35; 'classes': 0.35; 'good.': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'should': 0.36; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'skip:_ 30': 0.39; 'bill': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'called': 0.40; 'how': 0.40; 'above,': 0.60; 'most': 0.60; 'new': 0.61; "you're": 0.61; 'relatively': 0.65; 'here': 0.66; 'mar': 0.68; 'specialize': 0.74; 'abc': 0.84; 'believe,': 0.84; 'irrelevant': 0.84; 'presumably': 0.84; 'pytypeobject': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=0mbEFCzfogczkeU2e8W62zaOmBznmPVoXrHNJiThyYo=; b=UnJtpOHtqFUbUnUXTgJNWi0fmLNA7KX5dfHAJiUi2OpFfT7oNcY8BK4Yk/wi9Wgm/+ SxcqBDiG2MLsO20rUNSjGhLyOFB1kp0xSGqmzazm4hS2nvCDN8n4QfZ27oCEAwkiFwgd r92YTusOCrBDq5FgOPVl0llASd5/tBTevZcGH2lnoSzx94lNFEREYhJSbE6eKLEKHBli oOthx9YdsjOQLv5BapxCobjnootapxAptCd0BB1hz6I0tF88xx6LxhaeehIHEct54ghP QnhI7uyPPjMbSL6PdMj/PdMHecsy51H/8fhYOxdATYuCIYrxj9/mes+w/Ps3+7zFlrUo P/2g== X-Received: by 10.194.243.68 with SMTP id ww4mr4155113wjc.58.1393984545023; Tue, 04 Mar 2014 17:55:45 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <0ab424e9-3a3f-4111-9f41-ff50ce73d70d@googlegroups.com> References: <0ab424e9-3a3f-4111-9f41-ff50ce73d70d@googlegroups.com> From: Ian Kelly Date: Tue, 4 Mar 2014 18:55:04 -0700 Subject: Re: How to create an instance of a python class from C++ To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393984546 news.xs4all.nl 2851 [2001:888:2000:d::a6]:44328 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67775 On Tue, Mar 4, 2014 at 5:14 PM, Bill 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.