C++ Boost

Boost.Python

Header <boost/python/scope.hpp>


Contents

Introduction
Classes
Class scope
Class scope synopsis
Class scope constructors and destructor
Example

Introduction

Defines facilities for querying and controlling the Python scope (namespace) which will contain new wrapped classes and functions.

Classes

Class scope

The scope class has an associated global Python object which controls the Python namespace in which new extension classes and wrapped functions will be defined as attributes. Default-constructing a new scope object binds that object to the associated Python object. Constructing a is associated with , and

Class scope synopsis

namespace boost { namespace python
{
  class scope : public object, private noncopyable
  {
   public:
      scope(object const&);
      scope();
      ~scope()
  };
}}

Class scope constructors and destructor

scope(object const& x);
Stores a reference to the current associated scope object, and sets the associated scope object to the one referred to by x.ptr(). The object base class is initialized with x.
scope();
Stores a reference to the current associated scope object. The object base class is initialized with the current associated scope object. Outside any module initialization function, the current associated Python object is None.
~scope()
Sets the current associated Python object to the stored object.

Example

The following example shows how scope setting can be used to define nested classes.

C++ Module definition:

#include <boost/python/class.hpp>
#include <boost/python/scope.hpp>
using namespace boost::python;

struct X
{
  void f();

  struct Y { int g() { return 0; } };
};

BOOST_PYTHON_MODULE(nested)
{
   scope outer
       = class_<X>("X")
            .def("f", &X::f)
            ;
   class_<Y>("Y")
      .def("g", &Y::g)
      ;
}
Interactive Python:
>>> import nested
>>> y = nested.X.Y()
>>> y.g()
0

Revised 03 October, 2002

© Copyright Dave Abrahams 2002. All Rights Reserved.