cppyy: Automatic Python-C++ Bindings

https://news.ycombinator.com/rss Hits: 5
Summary

cppyy: Automatic Python-C++ bindings cppyy is an automatic, run-time, Python-C++ bindings generator, for calling C++ from Python and Python from C++. Run-time generation enables detailed specialization for higher performance, lazy loading for reduced memory use in large scale projects, Python-side cross-inheritance and callbacks for working with C++ frameworks, run-time template instantiation, automatic object downcasting, exception mapping, and interactive exploration of C++ libraries. cppyy delivers this without any language extensions, intermediate languages, or the need for boiler-plate hand-written code. For design and performance, see this PyHPC’16 paper, albeit that the CPython/cppyy performance has been vastly improved since, as well as this CAAS presentation. For a quick teaser, see Jason Turner’s introduction video. cppyy is based on Cling, the C++ interpreter, to match Python’s dynamism, interactivity, and run-time behavior. Consider this session, showing dynamic, interactive, mixing of C++ and Python features (there are more examples throughout the documentation and in the tutorial): >>> import cppyy >>> cppyy.cppdef(""" ... class MyClass { ... public: ... MyClass(int i) : m_data(i) {} ... virtual ~MyClass() {} ... virtual int add_int(int i) { return m_data + i; } ... int m_data; ... };""") True >>> from cppyy.gbl import MyClass >>> m = MyClass(42) >>> cppyy.cppdef(""" ... void say_hello(MyClass* m) { ... std::cout << "Hello, the number is: " << m->m_data << std::endl; ... }""") True >>> MyClass.say_hello = cppyy.gbl.say_hello >>> m.say_hello() Hello, the number is: 42 >>> m.m_data = 13 >>> m.say_hello() Hello, the number is: 13 >>> class PyMyClass(MyClass): ... def add_int(self, i): # python side override (CPython only) ... return self.m_data + 2*i ... >>> cppyy.cppdef("int callback(MyClass* m, int i) { return m->add_int(i); }") True >>> cppyy.gbl.callback(m, 2) # calls C++ add_int 15 >>> cppyy.gbl.callback(PyMyClass(1), 2) # calls Python-side override ...

First seen: 2025-07-16 14:09

Last seen: 2025-07-16 18:12