mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-22 05:05:11 +00:00
Docs: Fix several errors of examples from the doc (#592)
* [Doc] Fix several errors of examples from the doc * Add missing operator def. * Added missing `()` * Add missing `namespace`.
This commit is contained in:
parent
05bc1ffbe0
commit
9b815ad2e9
@ -72,7 +72,7 @@ functions:
|
|||||||
/* ... binding code ... */
|
/* ... binding code ... */
|
||||||
|
|
||||||
py::class_<MyClass>(m, "MyClass")
|
py::class_<MyClass>(m, "MyClass")
|
||||||
.def(py::init<>)
|
.def(py::init<>())
|
||||||
.def_readwrite("contents", &MyClass::contents);
|
.def_readwrite("contents", &MyClass::contents);
|
||||||
|
|
||||||
In this case, properties can be read and written in their entirety. However, an
|
In this case, properties can be read and written in their entirety. However, an
|
||||||
|
@ -186,7 +186,7 @@ example as follows:
|
|||||||
virtual std::string go(int n_times) = 0;
|
virtual std::string go(int n_times) = 0;
|
||||||
virtual std::string name() { return "unknown"; }
|
virtual std::string name() { return "unknown"; }
|
||||||
};
|
};
|
||||||
class Dog : public class Animal {
|
class Dog : public Animal {
|
||||||
public:
|
public:
|
||||||
std::string go(int n_times) override {
|
std::string go(int n_times) override {
|
||||||
std::string result;
|
std::string result;
|
||||||
@ -228,7 +228,8 @@ declare or override any virtual methods itself:
|
|||||||
|
|
||||||
class Husky : public Dog {};
|
class Husky : public Dog {};
|
||||||
class PyHusky : public Husky {
|
class PyHusky : public Husky {
|
||||||
using Dog::Dog; // Inherit constructors
|
public:
|
||||||
|
using Husky::Husky; // Inherit constructors
|
||||||
std::string go(int n_times) override { PYBIND11_OVERLOAD_PURE(std::string, Husky, go, n_times); }
|
std::string go(int n_times) override { PYBIND11_OVERLOAD_PURE(std::string, Husky, go, n_times); }
|
||||||
std::string name() override { PYBIND11_OVERLOAD(std::string, Husky, name, ); }
|
std::string name() override { PYBIND11_OVERLOAD(std::string, Husky, name, ); }
|
||||||
std::string bark() override { PYBIND11_OVERLOAD(std::string, Husky, bark, ); }
|
std::string bark() override { PYBIND11_OVERLOAD(std::string, Husky, bark, ); }
|
||||||
@ -242,11 +243,13 @@ follows:
|
|||||||
.. code-block:: cpp
|
.. code-block:: cpp
|
||||||
|
|
||||||
template <class AnimalBase = Animal> class PyAnimal : public AnimalBase {
|
template <class AnimalBase = Animal> class PyAnimal : public AnimalBase {
|
||||||
|
public:
|
||||||
using AnimalBase::AnimalBase; // Inherit constructors
|
using AnimalBase::AnimalBase; // Inherit constructors
|
||||||
std::string go(int n_times) override { PYBIND11_OVERLOAD_PURE(std::string, AnimalBase, go, n_times); }
|
std::string go(int n_times) override { PYBIND11_OVERLOAD_PURE(std::string, AnimalBase, go, n_times); }
|
||||||
std::string name() override { PYBIND11_OVERLOAD(std::string, AnimalBase, name, ); }
|
std::string name() override { PYBIND11_OVERLOAD(std::string, AnimalBase, name, ); }
|
||||||
};
|
};
|
||||||
template <class DogBase = Dog> class PyDog : public PyAnimal<DogBase> {
|
template <class DogBase = Dog> class PyDog : public PyAnimal<DogBase> {
|
||||||
|
public:
|
||||||
using PyAnimal<DogBase>::PyAnimal; // Inherit constructors
|
using PyAnimal<DogBase>::PyAnimal; // Inherit constructors
|
||||||
// Override PyAnimal's pure virtual go() with a non-pure one:
|
// Override PyAnimal's pure virtual go() with a non-pure one:
|
||||||
std::string go(int n_times) override { PYBIND11_OVERLOAD(std::string, DogBase, go, n_times); }
|
std::string go(int n_times) override { PYBIND11_OVERLOAD(std::string, DogBase, go, n_times); }
|
||||||
@ -373,7 +376,7 @@ crucial that instances are deallocated on the C++ side to avoid memory leaks.
|
|||||||
/* ... binding code ... */
|
/* ... binding code ... */
|
||||||
|
|
||||||
py::class_<MyClass, std::unique_ptr<MyClass, py::nodelete>>(m, "MyClass")
|
py::class_<MyClass, std::unique_ptr<MyClass, py::nodelete>>(m, "MyClass")
|
||||||
.def(py::init<>)
|
.def(py::init<>())
|
||||||
|
|
||||||
Implicit conversions
|
Implicit conversions
|
||||||
====================
|
====================
|
||||||
@ -487,6 +490,7 @@ to Python.
|
|||||||
.def(py::self += py::self)
|
.def(py::self += py::self)
|
||||||
.def(py::self *= float())
|
.def(py::self *= float())
|
||||||
.def(float() * py::self)
|
.def(float() * py::self)
|
||||||
|
.def(py::self * float())
|
||||||
.def("__repr__", &Vector2::toString);
|
.def("__repr__", &Vector2::toString);
|
||||||
|
|
||||||
return m.ptr();
|
return m.ptr();
|
||||||
|
@ -207,8 +207,8 @@ For instance, the following statement iterates over a Python ``dict``:
|
|||||||
void print_dict(py::dict dict) {
|
void print_dict(py::dict dict) {
|
||||||
/* Easily interact with Python types */
|
/* Easily interact with Python types */
|
||||||
for (auto item : dict)
|
for (auto item : dict)
|
||||||
std::cout << "key=" << item.first << ", "
|
std::cout << "key=" << std::string(py::str(item.first)) << ", "
|
||||||
<< "value=" << item.second << std::endl;
|
<< "value=" << std::string(py::str(item.second)) << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
It can be exported:
|
It can be exported:
|
||||||
|
@ -57,7 +57,7 @@ In C++, the same call can be made using:
|
|||||||
|
|
||||||
.. code-block:: cpp
|
.. code-block:: cpp
|
||||||
|
|
||||||
using pybind11::literals; // to bring in the `_a` literal
|
using namespace pybind11::literals; // to bring in the `_a` literal
|
||||||
f(1234, "say"_a="hello", "to"_a=some_instance); // keyword call in C++
|
f(1234, "say"_a="hello", "to"_a=some_instance); // keyword call in C++
|
||||||
|
|
||||||
Unpacking of ``*args`` and ``**kwargs`` is also possible and can be mixed with
|
Unpacking of ``*args`` and ``**kwargs`` is also possible and can be mixed with
|
||||||
|
@ -298,8 +298,8 @@ different kinds of input arguments:
|
|||||||
struct Pet {
|
struct Pet {
|
||||||
Pet(const std::string &name, int age) : name(name), age(age) { }
|
Pet(const std::string &name, int age) : name(name), age(age) { }
|
||||||
|
|
||||||
void set(int age) { age = age; }
|
void set(int age_) { age = age_; }
|
||||||
void set(const std::string &name) { name = name; }
|
void set(const std::string &name_) { name = name_; }
|
||||||
|
|
||||||
std::string name;
|
std::string name;
|
||||||
int age;
|
int age;
|
||||||
|
Loading…
Reference in New Issue
Block a user