Differences between revisions 3 and 4
Revision 3 as of 2002-11-15 20:41:08
Size: 1617
Editor: MikeRovner
Comment:
Revision 4 as of 2003-08-11 15:06:11
Size: 1618
Editor: grossetto
Comment: markup-o
Deletions are marked like this. Additions are marked like this.
Line 49: Line 49:
The scope is a class that has an associated global Python object which controls the Python namespace in which new extension classes and wrapped functions will be defined as attributes. Details can be found at["boost.python/scope"]. The scope is a class that has an associated global Python object which controls the Python namespace in which new extension classes and wrapped functions will be defined as attributes. Details can be found at ["boost.python/scope"].

def

Boost.Python now supports a free-function version of def which defines its function in the current scope:

  #include <boost/python.hpp>

  BOOST_PYTHON_MODULE(my_module)
  {
    def("name", function_ptr);
    def("name", function_ptr, call_policies);
    def("name", function_ptr, "documentation string");
    def("name", function_ptr, call_policies, "documentation string");
  }

etc.

scope

To get access to the current module, you can declare the current scope:

  #include <boost/python.hpp>

  BOOST_PYTHON_MODULE(my_module)
  {
    // set the docstring of the current module scope
    scope().attr("__doc__") = "my module's docstring";
    ...

    scope current;
    current.attr("x") = 1; // my_module.x = 1
  }

Of course, you can also set the current scope to any object:

  object some_obj;
  scope within(some_obj);
  def("foo", &foo); // define a function within some_obj as a namespace

and all subsequent definitions (classes, enums etc.) will go inside current then scope.

Be warned, however, that although you can set the current scope from a class_<> instance, the class_<>'s def() member function will work properly in some cases where the free-function def() cannot, since the latter is missing information about the class type being wrapped.

what is a scope?

The scope is a class that has an associated global Python object which controls the Python namespace in which new extension classes and wrapped functions will be defined as attributes. Details can be found at ["boost.python/scope"].

boost.python/module (last edited 2014-01-31 00:27:23 by PaulBoddie)

Unable to edit the page? See the FrontPage for instructions.