Lines Matching refs:Pet
10 custom C++ data structure named ``Pet``. Its definition is given below:
14 struct Pet {
15 Pet(const std::string &name) : name(name) { }
22 The binding code for ``Pet`` looks as follows:
31 py::class_<Pet>(m, "Pet")
33 .def("setName", &Pet::setName)
34 .def("getName", &Pet::getName);
47 >>> p = example.Pet('Molly')
49 <example.Pet object at 0x10cd98060>
75 <example.Pet object at 0x10cd98060>
79 suitable functionality in the ``Pet`` data structure, and it would be nice if
85 py::class_<Pet>(m, "Pet")
87 .def("setName", &Pet::setName)
88 .def("getName", &Pet::getName)
90 [](const Pet &a) {
91 return "<example.Pet named '" + a.name + "'>";
101 <example.Pet named 'Molly'>
116 py::class_<Pet>(m, "Pet")
118 .def_readwrite("name", &Pet::name)
125 >>> p = example.Pet('Molly')
132 Now suppose that ``Pet::name`` was a private internal variable
137 class Pet {
139 Pet(const std::string &name) : name(name) { }
153 py::class_<Pet>(m, "Pet")
155 .def_property("name", &Pet::getName, &Pet::setName)
176 >>> class Pet:
179 >>> p = Pet()
189 py::class_<Pet>(m, "Pet")
191 .def_readwrite("name", &Pet::name);
197 >>> p = example.Pet()
200 AttributeError: 'Pet' object has no attribute 'age'
207 py::class_<Pet>(m, "Pet", py::dynamic_attr())
209 .def_readwrite("name", &Pet::name);
215 >>> p = example.Pet()
239 struct Pet {
240 Pet(const std::string &name) : name(name) { }
244 struct Dog : Pet {
245 Dog(const std::string &name) : Pet(name) { }
255 py::class_<Pet>(m, "Pet")
257 .def_readwrite("name", &Pet::name);
260 py::class_<Dog, Pet /* <- specify C++ parent type */>(m, "Dog")
264 Alternatively, we can also assign a name to the previously bound ``Pet``
269 py::class_<Pet> pet(m, "Pet");
271 .def_readwrite("name", &Pet::name);
295 m.def("pet_store", []() { return std::unique_ptr<Pet>(new Dog("Molly")); });
300 >>> type(p) # `Dog` instance behind `Pet` pointer
301 Pet # no pointer downcasting for regular non-polymorphic types
303 AttributeError: 'Pet' object has no attribute 'bark'
306 type behind a base pointer, Python only sees a ``Pet``. In C++, a type is only
356 struct Pet {
357 Pet(const std::string &name, int age) : name(name), age(age) { }
366 Attempting to bind ``Pet::set`` will cause an error since the compiler does not
374 py::class_<Pet>(m, "Pet")
376 .def("set", static_cast<void (Pet::*)(int)>(&Pet::set), "Set the pet's age")
377 … .def("set", static_cast<void (Pet::*)(const std::string &)>(&Pet::set), "Set the pet's name");
383 >>> help(example.Pet)
385 class Pet(__builtin__.object)
389 | Signature : (Pet, str, int) -> NoneType
392 | 1. Signature : (Pet, int) -> NoneType
396 | 2. Signature : (Pet, str) -> NoneType
405 py::class_<Pet>(m, "Pet")
406 .def("set", py::overload_cast<int>(&Pet::set), "Set the pet's age")
407 .def("set", py::overload_cast<const std::string &>(&Pet::set), "Set the pet's name");
411 ``void (Pet::*)()`` as seen in the raw cast. If a function is overloaded based
433 py::class_<Pet>(m, "Pet")
434 .def("set", overload_cast_<int>()(&Pet::set), "Set the pet's age")
435 .def("set", overload_cast_<const std::string &>()(&Pet::set), "Set the pet's name");
454 struct Pet {
460 Pet(const std::string &name, Kind type) : name(name), type(type) { }
470 py::class_<Pet> pet(m, "Pet");
472 pet.def(py::init<const std::string &, Pet::Kind>())
473 .def_readwrite("name", &Pet::name)
474 .def_readwrite("type", &Pet::type);
476 py::enum_<Pet::Kind>(pet, "Kind")
477 .value("Dog", Pet::Kind::Dog)
478 .value("Cat", Pet::Kind::Cat)
481 To ensure that the ``Kind`` type is created within the scope of ``Pet``, the
489 >>> p = Pet('Lucy', Pet.Cat)
499 >>> Pet.Kind.__members__
511 >>> p = Pet( "Lucy", Pet.Cat )
514 Pet.Cat
516 'Pet.Cat'
529 py::enum_<Pet::Kind>(pet, "Kind", py::arithmetic())