[C++-sig] c++ bool to python bool

David Abrahams dave at boost-consulting.com
Tue Nov 1 15:58:48 CET 2005


"Nicolas P. Rougier" <Nicolas.Rougier at loria.fr> writes:

> Hello,
>
> This is certainly a newbie question but how do I get boost python to
> wrap a c++ bool type to a python bool type ?
>
> Here is what I tried:
>
> #include <boost/python.hpp>
> class Boolean {
> public:
>     Boolean (bool v) : value(v) {}
>     bool value;
> };
>
> BOOST_PYTHON_MODULE(boolean) {
>     using namespace boost::python;
>     class_<Boolean>("Boolean", init <bool>() ) 
>         .def_readwrite ("value", &Boolean::value);
> }
>
> But the value property is considered as being an 'int' in python (and
> it's important for me to get the right type). I saw that boost python
> changelog indicates support for python's bool type but I found no
> further documentation. Did I miss something ? 

It works for me; are you sure your Python actually includes bool
support?

        python -c "print bool"

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com




More information about the Cplusplus-sig mailing list