[C++-sig] module variable

Neal Becker ndbecker2 at gmail.com
Sun Jul 29 22:19:28 CEST 2007


David Abrahams wrote:

> 
> 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);
> }
> 

Thanks.  I got stuck on the fact that scope has a private assign, so you
can't assign to a global scope variable.  I guess you avoid this by
assigning to an object instead.





More information about the Cplusplus-sig mailing list