Commit 2952ea70 authored by Jørgen Lien Sellæg's avatar Jørgen Lien Sellæg
Browse files

add class example

parent 95509f60
......@@ -22,3 +22,6 @@ target_link_libraries(c-api ${Python3_LIBRARIES})
pybind11_add_module("Prime" pybind/heavy_example/module.cpp)
pybind11_add_module("CppMath" pybind/sum_example/module.cpp)
pybind11_add_module("Cpp" pybind/class_example/module.cpp)
add_subdirectory(pybind/embedded_example)
......@@ -27,7 +27,12 @@
- Slide 4
Using Pythons C/C++ API is hard.
- Slide 5
Using pybind11 makes it look like python code
- Slide 6
Lets look at some examples
- Overview of python
- Slide 7
Embedding the interpreter
#include <pybind11/operators.h>
#include <pybind11/pybind11.h>
#include <sstream>
namespace py = pybind11;
class point {
public:
int x = 0, y = 0, z = 0;
bool operator==(const point &left) const {
return x == left.x && y == left.y && z == left.z;
}
};
PYBIND11_MODULE(Cpp, m) {
py::class_<point>(m, "Point")
.def(py::init<>())
.def(py::self == py::self, "test")
.def_readwrite("x", &point::x)
.def_readwrite("y", &point::y)
.def_readwrite("z", &point::z)
.def("__repr__", [](const point &self) {
std::stringstream ss;
ss << "x: " << self.x << ", y: " << self.y << ", z: " << self.z;
return ss.str();
})
;
}
from sys import path
path.append("./../../build")
from Cpp import Point
p = Point()
p.x = 100
p.y = 100
p.z = 12
print(p)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment