Invoke C++ from Python without bindings thanks to ROOT

With ROOT, it's possible to use interactively C++ libraries from Python without the need to write bindings. The C++ entities known to the interpreter are exposed to Python transparently. Let's dive into an example.

The following sources include a class, a function and a template.

Header File

In [1]:
#include <iostream>
#include <typeinfo>

/// A trivial class
class A {

/// A trivial function
int CountCharacters(const std::string s);

/// A trivial template
template<class T>
class B {
      std::cout << "The typeid name of the template argument is " << typeid(T).name() << std::endl;
with open('myLibrary.h','w') as f_out:


In [2]:
#include "myLibrary.h"    
   std::cout << "This is the constructor of A" << std::endl;

   std::cout << "This is the destructor of A" << std::endl;

int CountCharacters(const std::string s)
   return s.size();
with open('','w') as f_out:

Creation of the Library

It's trivial to create a shared object starting from the sources above:

In [3]:
g++ -o -shared -fPIC
In [4]:
ls *so

So far, so good. Now we'll see how easy it is to use this library from within Python thanks to ROOT.

Interactivity without bindings

In order to interact with the C++ entities contained in the library, we need to carry out to tasks:

  1. We need to make known to the interpreter the interfaces. Concretely this means including one or more headers.
  2. We need to make accessible to the interpreter the implementations of such C++ entities. Concretely this means loading the library.

In code:

In [5]:
import ROOT
ROOT.gInterpreter.ProcessLine('#include "myLibrary.h"')
Welcome to JupyROOT 6.07/07

That's it! We can now start exploring the content of the library. If you are wondering what a return code equal to 0 means, ROOT is telling us that the loading of the library happened without problems!

In [6]:
a = ROOT.A()
This is the constructor of A
In [7]:
del a
This is the destructor of A
In [8]:
b_doublePtr = ROOT.B("double*")()
The typeid name of the template argument is Pd

Notice how the "impedence mismatch" generated by the concept of templates is ironed out in this case. The template parameter is specified as string in parentheses.

In [9]:
ROOT.CountCharacters("This interactivity without bindings is really impressive.")

Interactivity within C++

Interactive usage of C++ libraries is possible also in C++ mode. In this case, no wrapper is interposed between the statement written by the user and the call - ABI compatibility is leveraged. For example:

In [10]:
A a;
This is the constructor of A