C/C++ extension: subclassing an extension type with Python code?

Alex Martelli aleaxit at yahoo.com
Wed Mar 7 03:21:38 EST 2001


"Regular Expression" <xeger at usa.net> wrote in message
news:ytjp6.25967$jg1.598879 at news1.alsv1.occa.home.com...
> I'd like to create an extension type which can be further extended by end
> users of the type. Ideally, I'd like to define certain properties that are
> read-only, and others that cannot be occluded in "subclasses" of the type.
> I'd also like to define certain methods that cannot be overridden.
>
> Does anybody have an idea how to do this?

www.boost.org has an EXCELLENT start for you -- the Boost Python
Library lets you expose your C++ classes to Python in a way that
lets the Python users inherit from them (extend, override, &c).

It uses the deep and wondrous 'Beaudry Hook' for that purpose,
but, blessedly, you don't have to understand its intricacies to
just do what comes natural and Pythonic -- unimpeded subclassing.

You may have to dig very deep into the BPL internals to find ways
to selectively STOP overriding for certain things -- that's very
unPythonic, and liable to be a real mess.  But, at least you have
a start -- the BPL source code itself.  By just using BPL you'll
get all the real functionality -- 99% of your work will then be
in trying to make Python into what it isn't, a language which
protects the programmer against himself/herself (but, while you're
slaving away on that quixotic track, you may already release your
completely-functional code, "stopping" unintended overriding the
Pythonic way -- by DOCUMENTING that such-and-such method is not
to be overridden, naming it with a leading underscore if it's for
internal-use-only, and that's it).


Alex






More information about the Python-list mailing list