Python for C++ Developers

Last year in November 2020 I held a short one hour training session for my collegues at Altran on python. Because the majority of my collegues are C++ developers and I myself am a C++ and python developer I decided to make the python introduction to them more C++ friendly. Thus I decided to show a simple comparision of the functionality and data structures between Python and C++ so that my C++ collegues can relate more easily to the concepts in python.

Comparison

First I made a simple comparison with pros and cons between python and C++:


General

Python
  • General Purpose Language
  • Multi paradigm
C++
  • General Purpose Language
  • Multi paradigm

Advantages

Python
  • Easy to communicate ideas
  • Code readability
  • Package manager (PIP)
  • Scientific community sharing (open source)
  • Built-in Desgin Patterns (Singleton, ...)
  • Garbage collection
C++
  • Execution speed
  • Compiled
  • Statically typed
  • Modern fast professional libraries (QT, Eigen, ...)
  • Light Weight Libraries (Header only)

Disadvantages

Python
  • Slow (can be solved)
  • Interpreted (pro and con)
  • Dynamically typed (errors at runtime)
C++
  • Learning curve
  • Harder to access libraries (less sharing than in Python)
  • No garbage collection (can be pro or con)
  • No builtin design patterns

C++ and Python are both popular programming languages that have their own unique strengths and weaknesses.

C++ is a general-purpose programming language that was first developed in 1979. It is a statically typed language, which means that variables must be declared with a specific data type before they can be used in a program. C++ is known for its performance and efficiency, and is often used for building high-performance applications such as operating systems, games, and system drivers.

Python, on the other hand, is a high-level programming language that was first released in 1991. It is a dynamically typed language, which means that data types are inferred at runtime and variables can be reassigned to different data types. Python is known for its simplicity and readability, and is often used for web development, scientific computing, and data analysis.

In terms of performance, C++ is generally considered to be faster and more efficient than Python. This is because C++ is compiled directly to machine code, whereas Python is an interpreted language and is executed by the interpreter at runtime. However, the performance difference is not always significant, and in many cases, the simplicity and readability of Python can make it easier and faster to develop and maintain complex programs.

Overall, both C++ and Python are powerful and popular programming languages that have their own strengths and weaknesses. Whether you choose to use C++ or Python will depend on the specific needs and requirements of your project.

Interpreted vs Compiled

The next important difference between python and C++ is "Interpreted vs Compiled". To really understand the difference I found a good comparison analogy here which explains the difference perfectly:

Imagine you have a hummus recipe that you want to make, but it's written in ancient Greek. There are two ways you, a non-ancient-Greek speaker, could follow its directions. The first is if someone had already translated it into English for you. You (and anyone else who can speak English) could read the English version of the recipe and make hummus. Think of this translated recipe as the compiled version. The second way is if you have a friend who knows ancient Greek. When you're ready to make hummus, your friend sits next to you and translates the recipe into English as you go, line by line. In this case, your friend is the interpreter for the interpreted version of the recipe.

When to use Python/C++

Next I decided to give a simple overview on when to use each language:


Python
  • Implement fast prototypes or proof of concepts
  • Communicate ideas
  • Static data analysis (pandas)
  • Dynamic data analysis (pyspark)
  • Static matrix calculation (numpy)
  • Visualization of data (matplotlib)
  • Testing, Automation (Selenium), CI/CD, AirFlow
  • Private small games (pygame, OpenGL API)
  • Webapplications (Flask, Django)
  • Machine Learning (tensorflow, scikit-learn)
  • When you want to hack something quickly :)
C++
  • Professional Application which are performance or/and safety critical
  • Real time applications
  • Embedded Systems
  • Desktop Applications (QT)
  • 3D Modeling and complex dynamic matrix calculation (OpenGL API, Eigen)
  • Machine Learning (tensorflow C++ API)
  • Type safety important for the project
  • Multithreading Application
  • Professional Games (OpenGL API, Unreal)

The choice between using Python or C++ for a project will depend on a variety of factors, including the specific requirements and goals of the project, the performance and efficiency needs, and the preferences and experience of the development team.

In general, C++ is considered to be a good choice for building high-performance applications that require a lot of computational power, such as games, operating systems, and system drivers. C++ is also a good choice for applications that need to interact with other languages or systems, or for projects that require a high degree of control over memory management.

Python, on the other hand, is a good choice for projects that require a high degree of flexibility and rapid development. Python's simplicity and readability make it a good choice for projects that involve a lot of data processing or scientific computing, and its wide range of libraries and frameworks make it easy to build complex applications quickly. Python is also a good choice for projects that require a lot of collaboration or coordination, as its dynamic nature makes it easier for multiple developers to work together on the same codebase.

Ultimately, the choice between using Python or C++ will depend on the specific needs and goals of your project. It's important to carefully consider the requirements and constraints of the project, and to choose the language that is best suited to meeting those needs.

Enough talk

Because developers do not like talk and prefer some action with code I decided to do a live demo:

The full source code is available on github here. I opened both files (.cpp and .py) with the concepts side by side and introduced first the well known concept in C++ for example how to print "Hello World" and then showed the equivalent in python. In my repository you can see that I covered lots of concepts from variables, functions and object oriented programming. Of course I still just scratch the surface :). Feel free to open a pull request to add more concepts.

Calling Python from C++ (vice versa)

One interesting use case for all python and C++ developers is how to call functions from the other language. A C++ developer might need a library from python which is missing in C++ and a python developer might need the execution speed of C++. I heard in the game industry developers are using python for the game logic and C++ for the performance heavy stuff like graphics and matrix calculations. Thus I covered some common libraries like CPython, Cython and Boost.Python but in the end recommended pybind11 which is an awesome mature library which is header only. Iwan Smirnov did and awesome talk an that. Also check out some good pybind11 examples.

Welcome!

Welcome to my website. Here I share my knowledge, projects and interests.