defining class and subclass in C

Tim Roberts timr at probo.com
Sat Jan 14 20:26:51 EST 2012


Daniel Franke <franke.daniel at 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 at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list