Affiliate disclosure
Book titles on this page link to Amazon. As an Amazon Associate, DataField.Dev earns from qualifying purchases — at no additional cost to you.
Further Reading: Fortran-Python Interoperability
Where to go deeper on wrapping Fortran for Python. Sources are tagged Tier 1 (canonical and confidently recommended) and Tier 2 (real and worth seeking, but confirm the current edition/URL yourself). f2py is a moving target across NumPy versions, so for anything about the build backend trust the docs for the NumPy you actually have installed over any book, including this one.
The primary documentation (start here)
- The NumPy f2py user guide (in the official NumPy documentation). The authoritative reference: the
-c -mworkflow,intentanddependdirectives, signature (.pyf) files, the.f2py_f2cmapkind mapping, and — critically — the current build backend (distutils vs Meson). Read the section matching your NumPy version. Tier 1. - The NumPy C-API and "writing your own ufunc" pages. Background on what an extension module actually
is and how NumPy arrays are laid out in memory (
flags,strides, C- vs F-contiguity), which is the foundation under everything in §15.3. Tier 1. - The Python
ctypesdocumentation (Python standard library) and the cffi documentation. The two FFIs of §15.4, with the details ofrestype/argtypes,byref, and loading a shared library that this chapter only sketched. Tier 1 (ctypes) / Tier 2 (cffi).
The Fortran side of the boundary
- Metcalf, Reid & Cohen, Modern Fortran Explained (Oxford University Press). The precise rules for
iso_c_binding,bind(c), and interoperable kinds — the C face that ctypes and cffi require, and the chapter (14) this one builds on. Tier 1. - Milan Curcic, Modern Fortran (Manning). Written for exactly the reader of this chapter — someone who already programs in Python and wants Fortran's speed — with a modern, project-driven treatment of calling Fortran from other languages. Tier 1.
fortran-lang.org— "Interoperability" and the tutorials. Community-maintained, up-to-date examples of the f2py workflow and the modern toolchain around it; the best free starting point after the NumPy docs. Tier 1.
On the two-language workflow and honest performance
- The SciPy and NumPy source trees. The most instructive f2py examples in existence are the ones NumPy
and SciPy ship: real Fortran (including LAPACK wrappers) wrapped for Python at scale. Browsing
scipy/linalgshows the pattern in production. Tier 1. - "Why Python is slow" / interpreter-overhead explainers (talks and posts by CPython contributors). Background for §15.5: why a pure-Python element loop pays 10–100× — bytecode dispatch, boxing, dynamic typing — so the speedup is understood, not just observed. Tier 2 (verify the specific talk).
Tools
- Compiler Explorer (
godbolt.org). Selectgfortranand watch what the compiler makes of your kernel; by Chapter 27 this is where you will confirm the loop vectorized. Tier 1. mesonandninja. The build backend modern f2py uses on Python 3.12+. Install them (pip install meson ninja) before you are surprised by a build error. Tier 1.
Suggested order
- Read the NumPy f2py user guide for your installed version — especially the build-backend note —
then build
code/example-01-sum-squares.f90and confirm it imports and runs. - Do the §15.3 experiment: pass the same 2-D array to a kernel once as default (C-contiguous) and once as
order='F', and watch the timing change. The idea lands when you see it. - Skim the ctypes docs and rebuild
code/example-04-cfuncs.f90as a shared library — enough to appreciate what f2py automates. - Keep Metcalf/Reid/Cohen and the fortran-lang interoperability pages on hand as the references you return to when a kind won't map or a symbol won't resolve.