Fw: [C++-sig] Pyste and STL types

David Abrahams dave at boost-consulting.com
Wed Mar 19 01:49:29 CET 2003


Nicodemus <nicodemus at globalite.com.br> writes:

> This currently fails, apparently because of an internal mechanism of
> Boost.Python involving polimorphism. From what I gather from the error
> message, Boost.Python tries to derive from Test to check for
> polimorphic behaviour, and unions can't be subclassed. So, a possible
> solution would be to specialize a class_ to take in account the
> details of a union (lack of polimorphism, no derivation):
>
>     union_<Test>("Test")
>         .def_readwrite("d", &Test::d)
>         .def_readwrite("i", &Test::i)        ;

I just want to point out again that I don't think this is _usually_
the best way to map a union to Python.  For any union that could be
used effectively from Python there has to be information somewhere
saying which field is active.  When converting the union to Python, it
should use the information to convert the appropriate field to Python
instead.  When converting from python, it should set the information
up based on the type of the Python object.

A scheme like this requires some user intervention, but it smooths
over the difference between the language idioms.  Having something
which really acts like a C++ union is anti-idiomatic in Python.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com





More information about the Cplusplus-sig mailing list