# Python + C or Python + Fortran?

**URL:** <https://fortran-lang.discourse.group/t/python-c-or-python-fortran/2531>\
**Category:** Help\
**Created:** [January 7, 2022, 12:33pm UTC](https://fortran-lang.discourse.group/t/python-c-or-python-fortran/2531 "2022-01-07T12:33:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![CYehLu](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@CYehLu](https://fortran-lang.discourse.group/u/CYehLu)\
**Post date:** [January 7, 2022, 12:33pm UTC](https://fortran-lang.discourse.group/t/python-c-or-python-fortran/2531/1 "2022-01-07T12:33:55Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [January 7, 2022, 12:55pm UTC](https://fortran-lang.discourse.group/t/python-c-or-python-fortran/2531/2 "2022-01-07T12:55:25Z")

</div>

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.

> [@CYehLu](#):
>
> I think that Fortran and C are not mutually exclusive with each other,

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

---

<div class="post-metadata">

**Author:** ![nicholaswogan](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@nicholaswogan](https://fortran-lang.discourse.group/u/nicholaswogan)\
**Post date:** [January 7, 2022, 6:39pm UTC](https://fortran-lang.discourse.group/t/python-c-or-python-fortran/2531/3 "2022-01-07T18:39:51Z")

</div>

@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](https://github.com/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.](https://github.com/xtensor-stack/xtensor/issues/2269).

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

---

<div class="post-metadata">

**Author:** ![fortran4r](https://avatars.discourse-cdn.com/v4/letter/f/9de0a6/32.png) [@fortran4r](https://fortran-lang.discourse.group/u/fortran4r)\
**Post date:** [January 7, 2022, 9:06pm UTC](https://fortran-lang.discourse.group/t/python-c-or-python-fortran/2531/4 "2022-01-07T21:06:56Z")

</div>

For numerics, definitely use Fortran.

---

<div class="post-metadata">

**Author:** ![wyphan](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/wyphan/32/632_2.png) [@wyphan](https://fortran-lang.discourse.group/u/wyphan)\
**Post date:** [January 8, 2022, 12:21am UTC](https://fortran-lang.discourse.group/t/python-c-or-python-fortran/2531/5 "2022-01-08T00:21:12Z")

</div>

@Beliavsky @nicholaswogan I concur.  
My first reaction to seeing the OP is the [meme image “why not both”](https://knowyourmeme.com/memes/why-not-both-why-dont-we-have-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.

---

<div class="post-metadata">

**Author:** ![mskocic](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mskocic/32/3378_2.png) [@mskocic](https://fortran-lang.discourse.group/u/mskocic)\
**Post date:** [September 4, 2023, 4:59am UTC](https://fortran-lang.discourse.group/t/python-c-or-python-fortran/2531/6 "2023-09-04T04:59:12Z")

</div>

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](https://github.com/MilanSkocic/iapws).

---

<div class="post-metadata">

**Author:** ![snano](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@snano](https://fortran-lang.discourse.group/u/snano)\
**Post date:** [September 4, 2023, 8:38am UTC](https://fortran-lang.discourse.group/t/python-c-or-python-fortran/2531/7 "2023-09-04T08:38:32Z")

</div>

you can also try gfort2py [gfort2py](https://github.com/rjfarmer/gfort2py), if you mainly use gfortran compiler to call Fortran code from python in an elegant way. See the example here: [test](https://github.com/rjfarmer/gfort2py/tree/main/tests).
