1 [section boost/python/enum.hpp]
3 <boost/python/enum.hpp> defines the interface through which users expose their C++ enumeration types to Python. It declares the `enum_` class template, which is parameterized on the enumeration type being exposed.
5 [section Class template `enum_`]
6 Creates a Python class derived from Python's `int` type which is associated with the C++ type passed as its first parameter.
8 namespace boost { namespace python
11 class enum_ : public object
13 enum_(char const* name, char const* doc = 0);
14 enum_<T>& value(char const* name, T);
15 enum_<T>& export_values();
20 [section Class template `enum_` constructors]
21 ``enum_(char const* name, char const* doc=0);``
23 [[Requires][name is an [link ntbs] which conforms to Python's [@http://www.python.org/doc/current/ref/identifiers.html identifier naming rules].]]
24 [[Effects][Constructs an `enum_` object holding a Python extension type derived from `int` which is named `name`. The named attribute of the [link high_level_components.boost_python_scope_hpp current scope] is bound to the new extension type.]]
27 [section Class template `enum_` modifier functions]
28 ``enum_<T>& value(char const* name, T x);``
30 [[Requires][name is an [link ntbs] which conforms to Python's [@http://www.python.org/doc/current/ref/identifiers.html identifier naming rules].]]
31 [[Effects][adds an instance of the wrapped enumeration type with value x to the type's dictionary as the named attribute.]]
34 ``enum_<T>& export_values();``
36 [[Effects][sets attributes in the [link high_level_components.boost_python_scope_hpp current scope] with the same names and values as all enumeration values exposed so far by calling value().]]
43 #include <boost/python/enum.hpp>
44 #include <boost/python/def.hpp>
45 #include <boost/python/module.hpp>
47 using namespace boost::python;
49 enum color { red = 1, green = 2, blue = 4 };
51 color identity_(color x) { return x; }
53 BOOST_PYTHON_MODULE(enums)
57 .value("green", green)
62 def("identity", identity_);
67 >>> from enums import *
72 >>> identity(color.red)
78 >>> identity(color.green)
82 Traceback (most recent call last):
83 File "<stdin>", line 1, in ?
84 NameError: name 'blue' is not defined
86 >>> identity(color.blue)
89 >>> identity(color(1))
92 >>> identity(color(2))
95 >>> identity(color(3))
98 >>> identity(color(4))
102 Traceback (most recent call last):
103 File "<stdin>", line 1, in ?
104 TypeError: bad argument type for built-in operation