defining class and subclass in C

Stefan Behnel stefan_ml at behnel.de
Sun Jan 15 05:24:12 EST 2012


Daniel Franke, 14.01.2012 22:15:
> 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?!

You already got the response (and found for yourself) that this is normal.
CPython makes a distinction between classes defined the Python way and
extension types, the latter of which you define in your code.

As a general advice: if your primary interest is in implementing some kind
of functionality, instead of just learning about the bits and pieces of
CPython's C-API, you may want to take a look at Cython. It makes writing
efficient C extension modules fast and easy, especially when it comes to
class hierarchies and similarly things.

Stefan




More information about the Python-list mailing list