ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> cpp-sig
cpp-sig
Re: [C++-sig] wrap global variable (def not work)
by Nicodemus other posts by this author
Jun 4 2003 3:54PM messages near this date
[C++-sig] wrap global variable (def not work) | [C++-sig] boost::python and exceptions, seg fault
Zhang Le wrote:

> Hello, 
>   I want to wrap a single int variable to python model but failed:
>   the code is:
>   int verbose = 1;
>   BOOST_PYTHON_MODULE(foo)
>   {
>       def("verbose", &verbose);
>   }
> 
>   <snip error message>
> 
>     It seems def can only be used on class members. Any tips?
> 

Try this:

int verbose = 1;
int get_verbose() { return verbose; }
BOOST_PYTHON_MODULE(foo)
{
    scope().attr("verbose") = verbose;
    def("get_verbose", &get_verbose);
}

But there's one problem:

> >> import foo
> >> foo.verbose
1
> >> foo.verbose = 2
> >> foo.verbose
2
> >> foo.get_verbose()
1

Ie, changes in a global variable won't be seen in C++, so this technique works only for cons
tants. If you *really* need to be able to change the global variable from Python, you will h
ave to create accessor functions and use these. That's not Boost.Python fault really, is jus
t that in Python there's no way to know when a module-variable is *rebound* (note, not *chan
ged*).

HTH,
Nicodemus.




_______________________________________________
C++-sig mailing list
C++-sig@[...].org
http://mail.python.org/mailman/listinfo/c++-sig
Thread:
Zhang Le
Nicodemus

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved