[C++-sig] How to wrap class enums

Martin.Franzke at t-systems.com Martin.Franzke at t-systems.com
Wed Mar 12 09:20:04 CET 2014


Hi,

it works exactly the same as for old style enums, you only have to prepend the enum class scope (color::) to reference the values in the c++ code.

enum class color { red = 1, green = 2, blue = 4 };

color identity_(color x) { return x; }
BOOST_PYTHON_MODULE(enums)
{
    enum_<color>("color")
        .value("red", color::red)
        .value("green", color::green)
        .export_values()
        .value("blue", color::blue)
        ;

    def("identity", identity_);
}

What you can't do this way is to create new style python enums (see http://docs.python.org/3.4/library/enum.html), but as the documentation says, the python class is derived
from Python's int type.


Regards

Martin

Von: Cplusplus-sig [mailto:cplusplus-sig-bounces+martin.franzke=t-systems.com at python.org] Im Auftrag von Deepankar Sharma
Gesendet: Samstag, 8. März 2014 21:18
An: cplusplus-sig at python.org
Betreff: [C++-sig] How to wrap class enums

Any pointers on how to expose class enums to Python? Given the following enum what would the boost code to expose it to python look like? The page at http://www.boost.org/doc/libs/1_55_0/libs/python/doc/v2/enum.html only lists examples of old style enums.


enum class t_functions
{
            ADD, SUB, MUL, DIV
};
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20140312/34e40222/attachment.html>


More information about the Cplusplus-sig mailing list