Python Inheritance - A C example (without third party libraries)

Aaron Drew nospam at nospam.com
Sun May 13 05:09:12 EDT 2001


> >Can someone give me a source code example of how to derive a python class
> >(written in C) ....
>
> There are no classes in C. There is no "derivation" (sic) in C.

Yes. I realise that. I should have been more specific.

I am trying to mirror a C++ class heirarchy in python for scripting
purposes. I've written a python module (in C) with wrapper functions that
correspond to the class methods. I have even used Py_FindMethodInChain() to
allow calling of base class functions but I can't find any good documention
on the 'correct' way to do this. For example, my system doesn't allow me to
do typecasting from a derived type to a base class and I'm not entirely sure
that using Py_FindMethodInChains() is 'correct' (ie. the standard way to do
things).

I've tried browsing the source but I havn't had much luck finding anything
that fits my needs or explains what I should be doing.

>
> >from another python class (also written in C) such that...
>
> There are no classes in C.

Of course.

>
> >a. Derive further classes from it (in python)
>
> This is normal object behavior in python. See xxobject.c in the source
release
> for more details. I also recommend _Essential Python Reference_, which has

The xxobject template doesn't demonstrate how to check that an object is a
subclass of some other object. I can implement (in C/C++) a system that will
perform downcast checking for all of my C/C++ classes but that won't work
when I pass in a derived class type that was written solely in python.

> one of the better-written sections on Extending and Embedding that I've
ever
> seen. Native classes in Python are not characterized by any "inheritance"
> in the native C language (there is no inheritance in C)); rather, objects
are
> differentiated by their signature, the functions they offer, and the
mapping of
> the type name to a known glue structure. If you wish to write an extension
> object which "inherits" (sic) from another native-C Python object, you're
> going to have to do this the hard way or use a code-generator.

As I mentioned above, I can call base class methods perfectly at the moment.
I'm just looking for the 'correct' (ie. standardised) way to extend my C++
inheritance seamlessly with python.

>
> >b. Determine if a derived class is a subclass of a particular base class
(in
> >C)
>
> There are no classes in C. There are no derived, sub-, parrent-, or base-
> classes in C.

I get the picture. :)

>
> >I know there are resources such as CXX, SCXX and the boost library but
none
> >of these seem to make it clear and the python doc's aren't up to scratch
in
> >this area.
>
> _Essential Python Reference_ is good.

Thanks for the tip. I've just put a copy on order.

I think I've found what I'm looking for anyway. There is a tuple called
__bases__ that looks to store references to a classes base types. The
attribute cl_bases of PyClassObject points to this tuple. If I use
PyClassObject types for my wrappers and populate this tuple myself I think
it might just fit in easily with the existing python class code. Is this
right?

- Aaron





More information about the Python-list mailing list