defining class and subclass in C

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

Daniel Franke <franke.daniel at> 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 ''>
><type ''>
>Here I'd expect "<class ''>" instead?! And I never managed a proper 
>subclass :|

Notice this in your code:

static PyTypeObject ca_Type = {

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>" if you want.
Tim Roberts, timr at
Providenza & Boekelheide, Inc.

More information about the Python-list mailing list