classes.rst (11986:c12e4625ab56) classes.rst (12037:d28054ac6ec9)
1.. _classes:
2
3Object-oriented code
4####################
5
6Creating bindings for a custom type
7===================================
8

--- 24 unchanged lines hidden (view full) ---

33 py::class_<Pet>(m, "Pet")
34 .def(py::init<const std::string &>())
35 .def("setName", &Pet::setName)
36 .def("getName", &Pet::getName);
37
38 return m.ptr();
39 }
40
1.. _classes:
2
3Object-oriented code
4####################
5
6Creating bindings for a custom type
7===================================
8

--- 24 unchanged lines hidden (view full) ---

33 py::class_<Pet>(m, "Pet")
34 .def(py::init<const std::string &>())
35 .def("setName", &Pet::setName)
36 .def("getName", &Pet::getName);
37
38 return m.ptr();
39 }
40
41:class:`class_` creates bindings for a C++ `class` or `struct`-style data
41:class:`class_` creates bindings for a C++ *class* or *struct*-style data
42structure. :func:`init` is a convenience function that takes the types of a
43constructor's parameters as template arguments and wraps the corresponding
44constructor (see the :ref:`custom_constructors` section for details). An
45interactive Python session demonstrating this example is shown below:
46
47.. code-block:: pycon
48
49 % python

--- 243 unchanged lines hidden (view full) ---

293Sometimes there are several overloaded C++ methods with the same name taking
294different kinds of input arguments:
295
296.. code-block:: cpp
297
298 struct Pet {
299 Pet(const std::string &name, int age) : name(name), age(age) { }
300
42structure. :func:`init` is a convenience function that takes the types of a
43constructor's parameters as template arguments and wraps the corresponding
44constructor (see the :ref:`custom_constructors` section for details). An
45interactive Python session demonstrating this example is shown below:
46
47.. code-block:: pycon
48
49 % python

--- 243 unchanged lines hidden (view full) ---

293Sometimes there are several overloaded C++ methods with the same name taking
294different kinds of input arguments:
295
296.. code-block:: cpp
297
298 struct Pet {
299 Pet(const std::string &name, int age) : name(name), age(age) { }
300
301 void set(int age) { age = age; }
302 void set(const std::string &name) { name = name; }
301 void set(int age_) { age = age_; }
302 void set(const std::string &name_) { name = name_; }
303
304 std::string name;
305 int age;
306 };
307
308Attempting to bind ``Pet::set`` will cause an error since the compiler does not
309know which method the user intended to select. We can disambiguate by casting
310them to function pointers. Binding multiple functions to the same Python name

--- 107 unchanged lines hidden (view full) ---

418.. code-block:: pycon
419
420 >>> p = Pet('Lucy', Pet.Cat)
421 >>> p.type
422 Kind.Cat
423 >>> int(p.type)
424 1L
425
303
304 std::string name;
305 int age;
306 };
307
308Attempting to bind ``Pet::set`` will cause an error since the compiler does not
309know which method the user intended to select. We can disambiguate by casting
310them to function pointers. Binding multiple functions to the same Python name

--- 107 unchanged lines hidden (view full) ---

418.. code-block:: pycon
419
420 >>> p = Pet('Lucy', Pet.Cat)
421 >>> p.type
422 Kind.Cat
423 >>> int(p.type)
424 1L
425
426The entries defined by the enumeration type are exposed in the ``__members__`` property:
426
427
428.. code-block:: pycon
429
430 >>> Pet.Kind.__members__
431 {'Dog': Kind.Dog, 'Cat': Kind.Cat}
432
427.. note::
428
429 When the special tag ``py::arithmetic()`` is specified to the ``enum_``
430 constructor, pybind11 creates an enumeration that also supports rudimentary
431 arithmetic and bit-level operations like comparisons, and, or, xor, negation,
432 etc.
433
434 .. code-block:: cpp
435
436 py::enum_<Pet::Kind>(pet, "Kind", py::arithmetic())
437 ...
438
439 By default, these are omitted to conserve space.
433.. note::
434
435 When the special tag ``py::arithmetic()`` is specified to the ``enum_``
436 constructor, pybind11 creates an enumeration that also supports rudimentary
437 arithmetic and bit-level operations like comparisons, and, or, xor, negation,
438 etc.
439
440 .. code-block:: cpp
441
442 py::enum_<Pet::Kind>(pet, "Kind", py::arithmetic())
443 ...
444
445 By default, these are omitted to conserve space.