Python + C or Python + Fortran?

It seems that this problem should be posted at Python forum instead of a Fortran forum. But I think that most people at Python forum are not familiar to Fortran and asking here may be more appropriate.
The question is: when I want to speed up some procedures in Python, which one should I choose, C or Fortran?
(I want to focus on C vs Fortran and not consider other methods, e.g. numba, cython, etc )
In my experience, I prefer Fortran (with f2py) since it it more convenient to write. It is also faster in Fortran comparing to C, but I’m not sure if it just because I’m not very familiar to C. Consider that Python is implemented in C, I guess that maybe C is more suitable when working with Python in some situations.
I think that Fortran and C are not mutually exclusive with each other, i.e. I should choose Fortran in some situations and in others I should choose C. The problem is when should I choose Fortan, and when should I choose C?

Any suggestions are helpful. Thanks!


If the procedures use numerical arrays, especially in two or more dimensions, it may be easier to code them in Fortran. Would it be easy to code them in Matlab? If the procedures are working with strings, doing lots of I/O, and connecting to other libraries, C may be easier.

Not only that, they are interoperable – there is a standardized way to call one from the other.


@Beliavsky is right. Depends on problem. For scientific computing that often involves multidimensional arrays, I think the following are good options.

  1. Fortran + f2py. Only if you’re wrapping Fortran 90 features. f2py can not really expose object oriented Fortran to Python

  2. Fortran + Cython. You can wrap any Fortran with Cython, even derived types with type bound procedures. Examples: GitHub - Nicholaswogan/fortran-cython-examples

  3. C++ and xtensor and pybind11: Xtensor is a multidimensional array libray for C++. Makes C++ more like numpy. Wrapping with pybind11 is pretty easy.

Drawback of (1) is that f2py doesn’t wrap all of Fortran. Drawback of (2) is that you have to write the Cython wrapper, which is a bit tedious. Drawback of (3) is that xtensor has some performance issues for certain problems..

Also, yes. You can use Fortran, C and C++ all in the exact same project. They are all interoperable.


For numerics, definitely use Fortran.

@Beliavsky @nicholaswogan I concur.
My first reaction to seeing the OP is the meme image “why not both”.

To add to the discussion, the Fortran-C interoperability (and C++ if you do it correcty) revolves around two important things: BIND(C) and the ISO_C_BINDING module.

For intensive numerical computations, definitively Fortran. I write Fortran code and expose a C API with the iso_c_binding module.
I prefer writing by hand my Python wrappers over relying on Cython or f2py but that’s only a personal choice. An example on how it could be done, here.

1 Like

you can also try gfort2py gfort2py, if you mainly use gfortran compiler to call Fortran code from python in an elegant way. See the example here: test.