Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19001

Re: defining class and subclass in C

From Stefan Behnel <stefan_ml@behnel.de>
Subject Re: defining class and subclass in C
Date 2012-01-15 11:24 +0100
References <2921241.kWnIB6QQbB@silence>
Newsgroups comp.lang.python
Message-ID <mailman.4764.1326623066.27778.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: defining class and subclass in C Stefan Behnel <stefan_ml@behnel.de> - 2012-01-15 11:24 +0100

csiph-web