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.

樒偋ӃӚ⩑ﺍƐython׷ӃӲ㨃뗖䣩儹存㬍 胓▐༺쁋碗ൄЂ儀ຍꯊ

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 it to the associated global Python object. Constructing a scope object with an argument changes the associated global Python object to the one held by the argument, until the lifetime of the scope object ends, at which time the associated global Python object reverts to what it was before the scope object was constructed.

scope Ӑ趹؁굄ȫython攏㣬 膠ԏ㿘ֆPython儃뗖䣬 Ђ儀久 ꍷ◰ꯊ樒廑uז䖐儊䐔ᣍ ȱʡ鷫쏂㬽빹Ԭһ趐μļcode>scope攏㣬 ⢰㶨˼役؁굄ȫython攏㡣 ʹӃһ趲ΊԬscope攏㣬롸ı乘j儈뾖Python攏㣬 ᤳɸoΊ틹㖓Ĺ؁궔ϳ㬍 ֱ彼code>scope攏㵄ɺuƚ裬 䋊ᣬ閣굄ȫython攏㻘貴퍊 scope攏㹹Ԭ֮ǰ儗䌬ᣍ

Class scope synopsis

namespace boost { namespace python
{
  class scope : public object
  {
   public:
      scope(scope const&);
      scope(object const&);
      scope();
      ~scope()
   private:
      void operator=(scope const&);
  };
}}

Class scope constructors and destructor

explicit scope(scope const& x);
explicit 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.

ᣴ涔屇๘j儓ⶔϳ儒퓃㬍 ⢽빘j儓ⶔϳɨփΪx.ptr()˹ҽӃ億Ǹ擲攏㡣 object빀೵ʼ믎꼣ode>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.

ᣴ漘ǰ閣굄Ӳ攏㵄ҽӃᣍ object빀೵ʼ믎굱ǰ閣굄Ӳ攏㡣 ԚȎꎄ㿩㵊컯ꯊ햮͢㬵ᇰ閣굄Python攏㎪None

~scope()
Sets the current associated Python object to the stored object.

ɨփ屇๘j儐ython攏㎪ᣴ淨攏㡣

Example

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

ς=ϔʾKӲɨփȧꎓOڶ蒥Ƕ̗ ᣍ

C++ Module definition:

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

struct X
{
  void f() {}

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

BOOST_PYTHON_MODULE(nested)
{
   // add some constants to the current (module) scope
   scope().attr("yes") = 1;
   scope().attr("no") = 0;

   // Change the current scope 
   scope outer
       = class_<X>("X")
            .def("f", &X::f)
            ;

   // Define a class Y in the current scope, X
   class_<X::Y>("Y")
      .def("g", &X::Y::g)
      ;
}
Interactive Python:
>>> import nested
>>> nested.yes
1
>>> y = nested.X.Y()
>>> y.g()
42

Revised 09 October, 2002

© Copyright Dave Abrahams 2002.