# Fortran OOP from Python

**URL:** <https://fortran-lang.discourse.group/t/fortran-oop-from-python/3577>\
**Category:** Help\
**Created:** [May 28, 2022, 11:34am UTC](https://fortran-lang.discourse.group/t/fortran-oop-from-python/3577 "2022-05-28T11:34:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sabbraxcaddabra](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@sabbraxcaddabra](https://fortran-lang.discourse.group/u/sabbraxcaddabra)\
**Post date:** [May 28, 2022, 11:34am UTC](https://fortran-lang.discourse.group/t/fortran-oop-from-python/3577/1 "2022-05-28T11:34:30Z")

</div>

Hello everybody! I know how to use Fortran in Python with CFFI Python library and iso\_c\_binding fortran module. But now I think I want to use fortran OOP possibilities in Python. Is there any tools or I need to use some low-level wrapper in my fortran code like c language style?

---

<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:** [May 28, 2022, 11:57am UTC](https://fortran-lang.discourse.group/t/fortran-oop-from-python/3577/2 "2022-05-28T11:57:50Z")

</div>

There is [f90wrap](https://github.com/jameskermode/f90wrap), discussed in a paper [f90wrap: an automated tool for constructing deep Python interfaces to modern Fortran codes](https://iopscience.iop.org/article/10.1088/1361-648X/ab82d2), by James Kermode, and @rgoswami has blogged about [Simple Fortran Derived Types and Python](https://rgoswami.me/posts/cython-derivedtype-f2py/).

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [May 28, 2022, 12:21pm UTC](https://fortran-lang.discourse.group/t/fortran-oop-from-python/3577/3 "2022-05-28T12:21:45Z")

</div>

It depends on detailed the Python side needs insight into the Fortran library. The easy case is when you only want to use your Fortran objects in Python, as you only have to export all hooks via `bind(c)`. This step I’m at the moment doing mostly manually, because while well-defined, many objects have slightly different demands on the wrapper.

If you have a well-defined abstract base class you can implement this via a callback on the Fortran side, which takes C function and data pointer as implementation. I use this technique to implement logging for example. Viewing into Fortran data, dispatching an object based on its class or using memory allocators provided by a foreign runtime is more difficult. The former two are possible to implement, but can become a bit tedious, the latter I think is quite challenging with the current CFI features.

I gave a presentation on this topic a while ago, see

> [@Using objects across language boundaries](https://fortran-lang.discourse.group/t/using-objects-across-language-boundaries/2776):
>
> I’m giving a seminar next week on object-oriented programming in Fortran with focus on using Fortran objects in other languages, like C or Python. While the actual seminar is currently a closed round, we are sharing the presentations afterwards (or in advance). You can find my take on object-oriented programming in more than one language at [Using objects across language boundaries — Fortran OOP Seminar Series](https://awvwgk.github.io/foopss/). The source is available here: I’ll probably tinker on the presentation til Thursda…
