[C++-sig] Re: sub module support in V2

Dave Hawkes daveh at cadlink.com
Thu Jun 6 01:20:33 CEST 2002


"David Abrahams" <david.abrahams at rcn.com> wrote in message
news:186301c20cd3$2d84b5e0$6601a8c0 at boostconsulting.com...
>
> Yes, it looks like you'd either have to:
> 1. Adjust the system's idea of the module when it's added to the parent,
or
> 2. Defer Python module creation altogether until it's added to a parent
(or
> destroyed)
>     2a. Defer class __module__ attribute setting

I don't like either of these alternatives, so I'm thinking of a quite
different solution.

Instead of linking directly to the parent module, just define the sub-module
with a dotted name.

so we get:

boost::python::module("outer").def("Test1", Test1).def(...
boost::python::module("outer.inner1").def("Test2", Test2).def(...
boost::python::module("outer.inner2").def("Test3", Test3).def(...

This simplifies the chaining considerably as we can identify the parent
modules in sys.modules (and verify that it belongs to the current module).
An attempt to reuse a previous sub-module will just return a reference to
the existing one.

An advantage of this is that a later call to the main module would return a
reference to the previously created one and give more coding flexibility.

ie
boost::python::module("outer").def("Test1", Test1).def(...
boost::python::module("outer.inner").def("Test2", Test2).def(...
boost::python::module("outer").def("Test3", Test3).def(...    // get another
reference to the prior outer module
boost::python::module("outer.inner").def("Test4, Test4).def(...    // get
another reference to the prior inner module

Dave Hawkes










More information about the Cplusplus-sig mailing list