Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed5.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '#include': 0.05; 'received:localnet': 0.07; 'type,': 0.07; 'python': 0.08; '[1]:': 0.09; 'assumed': 0.09; 'null,': 0.09; 'struct': 0.09; 'subclass': 0.09; 'typedef': 0.09; 'case.': 0.15; 'explanation': 0.16; 'pyobject': 0.16; 'threw': 0.16; 'wrote:': 0.16; 'url:blog': 0.17; '>>>': 0.18; 'instance': 0.18; 'seems': 0.19; 'header:In-Reply- To:1': 0.22; 'obviously': 0.23; 'besides': 0.24; 'static': 0.24; 'code': 0.25; 'structure': 0.25; 'expect': 0.26; 'import': 0.27; 'tried': 0.27; 'allocated': 0.28; 'explicitly': 0.28; 'class': 0.29; 'null)': 0.30; 'skip:( 20': 0.31; 'received:209.85.214': 0.32; 'header:User-Agent:1': 0.33; 'instead': 0.33; 'to:addr :python-list': 0.33; 'daniel': 0.34; 'question': 0.35; 'url:python': 0.36; 'went': 0.36; 'charset:us-ascii': 0.36; 'two': 0.37; 'received:google.com': 0.37; 'skip:& 10': 0.37; 'url:ca': 0.38; 'some': 0.38; 'received:209.85': 0.38; "i'd": 0.39; 'sense': 0.39; 'received:209': 0.39; 'expected': 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.40; 'difference': 0.40; 'type': 0.60; 'lives': 0.63; 'below.': 0.63; 'here': 0.64; 'saturday': 0.68; 'flagged': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=from:to:subject:date:message-id:user-agent:in-reply-to:references :mime-version:content-transfer-encoding:content-type; bh=w5PMJf8r9yeIR7sQrI1X+M8RahvzauTbIk2zryyPTzI=; b=KrhUt/+u++e0q7Tscr7xHyWH4x0K3MXvz3sYbipMs0H2uUk7R4q8d6Oy8p9q4aVrYS M5+RrD1Co6TIo4p9nalPtQK/S2Pqv965MrEMWFZyeDygsZXksobMU5AokW76+siHUNlI TJevghv8D5UeuKR+UndR83d10tA+QI1c9hjbE= From: Daniel Franke To: python-list@python.org Subject: Re: defining class and subclass in C Date: Sun, 15 Jan 2012 00:29:20 +0100 User-Agent: KMail/4.7.3 (Linux/3.0.0-14-generic-pae; KDE/4.7.3; i686; ; ) In-Reply-To: <2921241.kWnIB6QQbB@silence> References: <2921241.kWnIB6QQbB@silence> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 88 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1326583786 news.xs4all.nl 6852 [2001:888:2000:d::a6]:48998 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18985 On Saturday 14 January 2012 22:15:36 Daniel Franke wrote: > Here I'd expect "" instead?! And I never managed a proper > subclass :| Found an explanation on type/class at [1]: "he difference between the two is whether the C-level type instance structure is flagged as having been allocated on the heap or not" - hu? With this info, I went ahead and tried the code quoted below. Now I get: >>> import cmod >>> cmod.ca >>> cmod.cb >>> cmod.ca.__bases__ (,) >>> cmod.cb.__bases__ (,) which seems to make sense besides the "type" instead of "class"?! What threw me is that I expected that I'd need to explicitly subclass the "object" type, from which I assumed for some reason I'd also inherit the .tp_new member. This is obviously not the case. Now the last question is, how do I get the a_type of Python class to use it as base for "cb"? On lives and learns. Daniel [1] http://utcc.utoronto.ca/~cks/space/blog/python/ClassesAndTypes -- #include typedef struct { PyObject_HEAD } ca; static PyTypeObject ca_Type = { PyObject_HEAD_INIT(NULL) }; typedef struct { PyObject_HEAD } cb; static PyTypeObject cb_Type = { PyObject_HEAD_INIT(NULL) }; PyMODINIT_FUNC initcmod(void) { PyObject *cmod; ca_Type.tp_name = "cmod.ca"; ca_Type.tp_basicsize = sizeof(ca); ca_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE; ca_Type.tp_new = PyType_GenericNew; ca_Type.tp_base = &PyBaseObject_Type; if (PyType_Ready(&ca_Type) < 0) return; cb_Type.tp_name = "cmod.cb"; cb_Type.tp_basicsize = sizeof(cb); cb_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE; cb_Type.tp_base = &ca_Type; if (PyType_Ready(&cb_Type) < 0) return; cmod = Py_InitModule3("cmod", NULL, "c module"); if (cmod == NULL) return; Py_INCREF(&ca_Type); PyModule_AddObject(cmod, "ca", (PyObject*)&ca_Type); Py_INCREF(&cb_Type); PyModule_AddObject(cmod, "cb", (PyObject*)&cb_Type); }