[C++-sig] module variable

David Abrahams dave at boost-consulting.com
Sun Jul 29 19:30:22 CEST 2007


on Sun Jul 29 2007, Neal Becker <ndbecker2-AT-gmail.com> wrote:

> I can create a module variable like this:
>
> static func_that_needs_x () {
> // How do I access the value of x?
> }
>
> BOOST_PYTHON_MODULE () {
>   scope().attr ("x") = 2;
>   def ("func", &func_that_needs_x);
> }
>
> But suppose I have a c++ function that needs to access the value?  How do I
> do this?

object module;
int get_x()
{
    return extract<int>(module.attr("x"))
}

BOOST_PYTHON_MODULE () {
  module = scope()
  scope().attr ("x") = 2;
  def ("func", &func_that_needs_x);
}

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

The Astoria Seminar ==> http://www.astoriaseminar.com




More information about the Cplusplus-sig mailing list