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

inital commit

\ No newline at end of file
# Python meetup
## 25.04.18
- Slide 1
Sportradar is sponsoring the talk
Creating python modules with modern C++
- Slide 2
First of, I'm no python developer, I joined this meetup group to learn about
this growing language.
I wanted to do a talk, so I had to figure out a way to talk about C++
to an Python audience.
SoMe handles
- Slide 3
Why do you want to write Python modules in C++?
- get an understanding of how python is implemented
- speed up CPU/GPU heavy calculations
- be able to use an C/C++ library in Python
- create a python API for your C++ application
- Slide 4
Using Pythons C/C++ API is hard.
- Overview of python
IndentWidth: 2
AccessModifierOffset: -2
UseTab: Never
ColumnLimit: 0
NamespaceIndentation: All
cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y -Wall -Wextra")
find_package(Python3 COMPONENTS Development)
add_library(c-api main.cpp)
target_include_directories(c-api PUBLIC ${Python3_INCLUDE_DIRS})
target_link_libraries(c-api ${Python3_LIBRARIES})
#include <Python.h>
int sum_ints(int a, int b) {
return a + b;
static PyObject *bind_sum(PyObject *, PyObject *args) {
int a, b;
if (!PyArg_ParseTuple(args, "ii", &a, &b))
return NULL;
return PyLong_FromLong(sum_ints(a, b));
static PyObject *SumError;
static PyMethodDef math_methods[] = {
{ "sum", bind_sum, METH_VARARGS, "Sum two ints" }
static struct PyModuleDef math_module = {
PyMODINIT_FUNC PyInit_Maths(void) {
PyObject *m = nullptr;
m = PyModule_Create2(&math_module, 1013);
if (!m) {
return nullptr;
SumError = PyErr_NewException("sum.error", nullptr, nullptr);
PyModule_AddObject(m, "error", SumError);
return m;
from distutils.core import setup, Extension
module1 = Extension('Maths',
sources = ['main.cpp'])
setup (name = 'Maths',
version = '1.0',
description = 'docs',
ext_modules = [module1])
from subprocess import run
run(["python", "", "build"]) # look away
from sys import path
path.append("./build/lib.linux-x86_64-3.7") # sorry
from Maths import sum
result = sum(3, 2)
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