How C++ and Python can thrive together


Shyamnath Premnadh

Hi, there 👋


Shyamnath Premnadh aka Shyam
Qt for Python
Senior Software Engineer @ TQtc

Why is C++ so loved ?

  • Extremely fast
  • Low level control
  • Multi paradigm
  • Highly mature
  • Strong community

Would you turn to C++ for being
quick and dirty ? 🤔

Considerations for a C++ project

  • CMake
  • Preprocessor Directives
  • Memory Management eg: raw_pointer, unique_pointer, shared_pointer
  • Templates
  • Design Patterns eg: CRTP, Singleton, Visitor
  • Meta-programming
  • Overload resolution
  • lvalue / rvalue / xvalue

... and many more

So, what do we do ? 😶‍🌫️

Python to the rescue 🐍

Python popularity (1/2)

Python popularity (2/2)

The 2021 update
  • 3rd Most Popular 📈
  • 6th Most Loved 📉
  • 1st Most Wanted 🎉
  • 1st Tiobe Index 🎉

One can argue, those numbers are not really representative.

Applications of Python

Syntax


            # Python
            print("Python goes b" + "r"*10)
          

            // C++
            #include <iostream>
            #include <string>

            int main()
            {
              std::cout << "C++ goes b" <<  std::string(10,"r");
            }
          

            # Python
            name = ["hello", "world"]
            is_hello = True if "hello" in name else False
          

            // C++
            auto name = {"hello", "world"}
            bool is_hello;
            if (std::find_if(name.begin(), name.end(), "hello"))
              is_hello = True;
            else
              is_hello = False;
          

            # Python

            # join a list of strings
            words = ["this", "is", "a", "sentence"]
            sentence = ' '.join(words)
          

            //C++
            auto words = {"This", "is", "a", "sentence"};
            auto sentence = std::accumulate(words.begin(), words.end(), std::string{});
            

Relation with C++

Qt for Python

What you might know about Qt for Python

The official set of Python bindings for the Qt framework.

...but not only that 🤔.

Things that /maybe/ you didn't know 😮

Qt for Python is also an application suite

How do we bring C++ into Python world?

More videos: https://doc.qt.io/qtforpython/videos.html

How do we bring Python into C++ world?

Let's look at some code 🤖

QtScrypt (1/2)

  • Inspired by QtScript, not a port
  • A dynamic way to interact with Python from C++
  • Enabling Python modules within C++ 🐍
  • proof of concept

QtScrypt (2/2)


// [0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
e.execute("print([i**3 for i in range(10)])");

// QVariant(QString, "Hello This Is A Test")
qDebug() << e.evaluate("f'hello this is a test'.title()");

//QVariant(double, 4950)
qDebug() << e.evaluate("sum(i for i in range(100))")

QScryptModule mod("super"); // super.py
QScryptFunction f1("add_three_numbers", &mod);

QVariantList args1;
args1 << 5 << 6 << 9;
// QVariant(double, 20)
qDebug() << f1.call(args1);
          

What about speed ?🏃

Not only vanilla Python 🍨

  • Python implementation (in Python)
  • Has a JIT
  • On average 4.2 faster than CPython
  • Mandelbrot Example: 10 times faster than CPython

Deployment 🚢

...I hear Kde likes Flatpak 🧐

pyside6-deploy(1/2)

  • Desktop Deployment
    • Normally uses Nuitka
    • But, also supports Flatpak
  • What is Nuitka?
    • Python Compiler
    • Translate Python to C -> compiled -> linked

pyside6-deploy - Nuitka(2/3)

pyside6-deploy - Flatpak(3/3)

Whats in it for Kde ?

  • Python plugins for Krita
  • PyFalkon - python bindings for Falkon
  • PySide6 Qml code with Kirigami and Plasma

Resources

Communication channels

More platforms at wiki.qt.io/Qt_for_Python#Community

How C++ and Python can thrive together


Shyamnath Premnadh