Inheritance in extension types

Martin von Loewis loewis at informatik.hu-berlin.de
Tue Sep 11 05:48:54 EDT 2001


Adrien Hernot <amh at BSD-DK.dk> writes:

> I want to export several C++ classes to python. I succeded for most
> of the functionalities I want, but for one: inheritance. That is, I
> would like the wrapper to class A to be able to access function in
> class B if B is a base of A. Also, I would like a function accepting
> B as argument to be able to accept A. For that I need inheritance
> information for my wrappers to use.

I don't believe that statement is true. Python wrappers are not
classes, so they don't inheritance. OTOH, you don't need inheritance
in Python to get polymorphism: two objects can be processed by the
same routine even if they don't share a common base type, as long as
they offer the same attributes.

If, by "classes", you mean "Python types", and by "function", you mean
"Python functions" (as opposed to C++ functions), then I can see no
reason why the types should inherit.

>   I can see easy ways to do that, but I guess there is a 'supported'
> way of doing thoses things.

In Python 2.1 and before, the clear answer is "no". Types don't
inherit.

> I saw in the PyTypeObject structure a zone reserved to 'Attribute
> descriptor and subclassing stuff' (as the comment says,
> Include/object.h l.275).  To be precise, the fields tp_base and
> tp_bases seems to be what I need.

Note that this will be released with Python 2.2 only. Also, I very
much doubt that this is what you need. I guess you can easily do
without it.

>   Is it possible to set A's tp_base (tp_bases) to point to B so that
> magically if an attribute is not found by A's getattr(), python will
> call B's getattr() ?

I think so. However, you might need to go through type_new to create
such a type, plus you have to use PyObject_GenericGetAttr.

>   Is it possible to set A's tp_base (tp_bases?) to point to B so
> that my hand written getattr() for A will also call any object found
> in A's tp_base ?

No. You'll have to use PyObject_GenericGetAttr.

>   What are those fields used for ?

They are still under developement and not documented, so you best
inspect the code for their usage.

>   Also, looking through the python sources, I found a PyClass_Type
> definition.  As of now, my wrappers declare new instances of
> PyTypeObject (one per wrapped class) and in the init function of the
> module, I explicitly set the ob_type field to PyType_Type.  Could
> not I set it to PyClass_Type and benefit from new python
> functionalities ?

No. PyClass_Type are for old-style classes only; those don't allow
C-defined methods.

>   Please keep in mind that I don't master python at all and that the
> python internals are even more obscure to me.  (I am not part of the
> mailing list, so please CC me in replies)

Then I strongly advise to restrict yourself to what is available in
Python 2.1. It should be easy enough to solve your problem without
type inheritance.

Regards,
Martin




More information about the Python-list mailing list