Learning Python & C++: Is It Easy?

  • Context: C/C++ 
  • Thread starter Thread starter Winzer
  • Start date Start date
  • Tags Tags
    Python
Click For Summary

Discussion Overview

The discussion revolves around the transition from learning Python to C++, particularly in the context of computational software and libraries used in fields like computational fluid dynamics (CFD). Participants explore the advantages and challenges of using C++ alongside Python, as well as tools for interfacing the two languages.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants express enthusiasm for learning Python and inquire about the ease of transitioning to C++ afterward.
  • Others suggest that programmers coming from higher-level languages may write better C++ code compared to those with a background in lower-level languages like C or Fortran.
  • A participant notes the importance of C++ for certain computational software, such as OpenFOAM for CFD applications.
  • There is a discussion about using Boost to interface Python with C++ programs, with questions about the feasibility of using Python for OpenFOAM.
  • Some argue that C++ is beneficial for intensive computations, providing better performance compared to Python for certain tasks.
  • Participants mention the possibility of using Python wrappers for existing CFD libraries, suggesting that C++ can handle heavy computations while Python can be used for higher-level programming.
  • Concerns are raised about Boost::Python, with one participant expressing dissatisfaction and suggesting SWIG as an alternative for wrapping C/C++ code for use with Python.
  • Another participant supports the use of Langtangen's book as a resource for understanding how to interface Python with Fortran and C/C++ code.
  • There is a debate regarding the effectiveness of SWIG versus Ctypes for wrapping C/C++ code, with differing opinions on their usability and performance.

Areas of Agreement / Disagreement

Participants express a mix of opinions regarding the transition from Python to C++, the effectiveness of various interfacing tools, and the best practices for using these languages in computational contexts. No consensus is reached on the superiority of one tool over another or the best approach to learning C++ after Python.

Contextual Notes

Some discussions highlight limitations in the capabilities of Boost::Python and SWIG, as well as the challenges of using C++ with Python, particularly for beginners. The conversation reflects varying levels of experience and familiarity with programming concepts and tools.

Winzer
Messages
597
Reaction score
0
I am learning Python right now and it is pretty cool. Just out of curiosity, would it be a easy transition to learn C++ after I master Python?
 
Technology news on Phys.org
Why would you want to!
Often people coming from a higher level language write better C++ code (using boost and STL) rather than people coming from C/Fortran who write at too low a level.
 
Boost and STL, never heard of them. I am new to programming:)

Well, I have noticed some really good computational software that requires C++, like open foam for CFD.
I would like to eventually get into these types of programs.
 
So boost allows me to interface python with c++ programs, is that correct?
So I would be able to use python for open foam?
 
If you do a lot of intense computations such as CFD, it does not hurt to start programming in C++ which gives you a lot more return for your CPU seconds.
Python and other high level languages make excellent prototyping tools.
 
If you have an existing CFD (or whatever) library it might be worth looking for a Python wrapper, or making some wrappers using boost::python.
Let C++ (or Fortran) do the heavy lifting an write your own program on top of this in Python - you get a lot better value for your programming hours and a reduction in all-night bug hunts.
 
For what little it's worth, I've often found Boost::Python to be perhaps the most severely lacking component in the Boost libraries. (The reasons are technical and I won't discuss them here.)

On the other hand, Swig is a great way of wrapping C/C++ code so that it can be used with Python (among many other languages). It's discussed in several places but a really useful introduction to it, as well as to the use of Python more generally in a scientific context, is Langtangen's https://www.amazon.com/dp/3540739157/?tag=pfamazon01-20. (Langtangen's book is wonderful for many reasons but it also does a really good job of illustrating how Python can be used to interface with Fortran and C/C++ code, something that's essential knowledge if you're going to be working with scientific libraries.)
 
Last edited by a moderator:
Thanks for the book pointer,
I've noticed a couple of projects switching back to SWIG. When I last used it I remember it generated a very 'c' like interface, I've only used boost:python for a few trial projects.
 
mgb_phys said:
Thanks for the book pointer,

I really can't recommend it highly enough. It takes a bit of effort, but there's an enormous amount of useful information in there, particularly regarding Numpy and wrapping Fortran or C/C++ code in Python. It's a gem of a book.
 
  • #10
shoehorn said:
For what little it's worth, I've often found Boost::Python to be perhaps the most severely lacking component in the Boost libraries. (The reasons are technical and I won't discuss them here.)

On the other hand, Swig is a great way of wrapping C/C++ code so that it can be used with Python (among many other languages). It's discussed in several places but a really useful introduction to it, as well as to the use of Python more generally in a scientific context, is Langtangen's https://www.amazon.com/dp/3540739157/?tag=pfamazon01-20. (Langtangen's book is wonderful for many reasons but it also does a really good job of illustrating how Python can be used to interface with Fortran and C/C++ code, something that's essential knowledge if you're going to be working with scientific libraries.)
I have Langtangen and agree it's excellent. I'd disagree w/ the recommendation of Swig now, especially for those starting out. Ctypes IMO has eclipsed it as the best bet: far easier to use, learn, and debug, comes builtin w/ Python, and for some things out performs Swig substantially. Downside is Ctypes won't handle C++
 
Last edited by a moderator:

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
6
Views
3K
  • · Replies 10 ·
Replies
10
Views
4K
Replies
6
Views
3K
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K