Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #18979 > unrolled thread
| Started by | Daniel Franke <franke.daniel@gmail.com> |
|---|---|
| First post | 2012-01-14 22:15 +0100 |
| Last post | 2012-01-14 17:26 -0800 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
defining class and subclass in C Daniel Franke <franke.daniel@gmail.com> - 2012-01-14 22:15 +0100
Re: defining class and subclass in C Tim Roberts <timr@probo.com> - 2012-01-14 17:26 -0800
| From | Daniel Franke <franke.daniel@gmail.com> |
|---|---|
| Date | 2012-01-14 22:15 +0100 |
| Subject | defining class and subclass in C |
| Message-ID | <mailman.4751.1326575767.27778.python-list@python.org> |
Hi all.
I spent some days and nights on this already and my google-fu is running out.
I'd like to implement the equivalent of this Python code in a C-extension:
>>> class A(object):
... pass
>>> class B(A):
... pass
>>> A
<class '__main__.A'>
>>> B
<class '__main__.B'>
>>> B.__bases__
(<class '__main__.A'>,)
However, loading my C-code (quoted below) I get:
>>> import ca
>>> ca
<module 'ca' from 'ca.so'>
>>> ca.ca
<type 'ca.ca'>
Here I'd expect "<class 'ca.ca'>" instead?! And I never managed a proper
subclass :|
The point of the excercise: with "ca" and "cb" properly implemented, I'd like
to subclass "cb" not from "ca", but from the Python "class A" - if possible?!
Could somepne kindly point out my mistake(s) and set me back on track?
Thanks
Daniel
--
#include <Python.h>
typedef struct {
PyObject_HEAD
} ca;
static PyTypeObject ca_Type = {
PyObject_HEAD_INIT(NULL)
};
PyMODINIT_FUNC initca(void) {
PyObject *ca;
ca_Type.tp_name = "ca.ca";
ca_Type.tp_basicsize = sizeof(ca);
ca_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
if (PyType_Ready(&ca_Type) < 0)
return;
ca = Py_InitModule3("ca", NULL, "ca module");
if (ca == NULL)
return;
Py_INCREF(&ca_Type);
PyModule_AddObject(ca, "ca", (PyObject*)&ca_Type);
}
$ gcc -Wall -Wextra -g -fPIC -I/usr/include/python2.7 ca.c \
-shared -Wl,-soname,ca.so -o ca.so -lpython2.7
[toc] | [next] | [standalone]
| From | Tim Roberts <timr@probo.com> |
|---|---|
| Date | 2012-01-14 17:26 -0800 |
| Message-ID | <kia4h7pculm1ap86o6mr49op75nmol90in@4ax.com> |
| In reply to | #18979 |
Daniel Franke <franke.daniel@gmail.com> wrote:
>
>I'd like to implement the equivalent of this Python code in a C-extension:
>
>>>> class A(object):
>... pass
>>>> class B(A):
>... pass
>>>> A
><class '__main__.A'>
>>>> B
><class '__main__.B'>
>>>> B.__bases__
>(<class '__main__.A'>,)
>
>However, loading my C-code (quoted below) I get:
>
>>>> import ca
>>>> ca
><module 'ca' from 'ca.so'>
>>>> ca.ca
><type 'ca.ca'>
>
>Here I'd expect "<class 'ca.ca'>" instead?! And I never managed a proper
>subclass :|
Notice this in your code:
static PyTypeObject ca_Type = {
PyObject_HEAD_INIT(NULL)
};
You are creating a "type" object. It shouldn't be a surprise that it is
displayed as a <type>, just like int and dict.
In a sweeping overgenerality, C modules define types and Python modules
define classes. You could redefine the __repr__ method to display "<class
ca.ca>" if you want.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web