Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
Open sidebar
Jørgen Lien Sellæg
python-meetup-python-modules-modern-cpp
Commits
2952ea70
Commit
2952ea70
authored
Apr 25, 2019
by
Jørgen Lien Sellæg
Browse files
add class example
parent
95509f60
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
51 additions
and
2 deletions
+51
-2
CMakeLists.txt
CMakeLists.txt
+3
-0
README.md
README.md
+7
-2
pybind/class_example/module.cpp
pybind/class_example/module.cpp
+29
-0
pybind/class_example/test.py
pybind/class_example/test.py
+12
-0
No files found.
CMakeLists.txt
View file @
2952ea70
...
...
@@ -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
)
README.md
View file @
2952ea70
...
...
@@ -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
pybind/class_example/module.cpp
0 → 100644
View file @
2952ea70
#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
();
})
;
}
pybind/class_example/test.py
0 → 100644
View file @
2952ea70
from
sys
import
path
path
.
append
(
"./../../build"
)
from
Cpp
import
Point
p
=
Point
()
p
.
x
=
100
p
.
y
=
100
p
.
z
=
12
print
(
p
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment