Already from a philosophical standpoint, the second-order or polymorphic λ-calculus presents circularity, but we can immediately note that

We are used to circularities in definitions of recursive functions and types, so we should not abandon hope about the system.

from chapter 9 of Type Theory & Functional Programming of Simon Thompson

In math topology we study cut points to define the connected spaces and homeomorphisms and that is the theoretical basis for the solution I’m proposing here in python

But what is the correct practical approach? They usually say you need modularity and many trendy buzzwords follow like microservices, but I present you a simpler, more effective way of programming.

Well, the 3 files circle1.py, circle2.py and circle3.py are

# circle1.py
A = 1
# circle2.py
from circle1 import A
B = A + 1
from circle3 import C
print(C)
# circle3.py
from circle2 import B
C = B + 1
D = 'imported!'

Now, if you try importing circle3 you get a worrying circular import error. But which is the solution? Ask a philosopher or a mathematician. That is simply a disconnection due to a cut point, it is the philosophical fact that

lines are not homeomorphic to circles

From Wikipedia: Cut-point

Programmer’s solution: cut “and paste“: you simply need to import circle2 before circle3! Easy like that!

One thought on “Circularity: philosophical types and real-world python”

The philosophical part consists of my opinion on how to resolve the circular problem: you can prefer the modular approach for large projects and teams while you can leverage the order convention for small projects or teams.

The philosophical part consists of my opinion on how to resolve the circular problem: you can prefer the modular approach for large projects and teams while you can leverage the order convention for small projects or teams.

LikeLike