Circular dependencies

One of the annoying problems that you are likely to face while working with modules is what is known as circular dependencies. To understand what these are, consider the following two modules:

# module_1.py

from module_2 import calc_markup

def calc_total(items):
    total = 0
    for item in items:
        total = total + item['price']
    total = total + calc_markup(total)
    return total

# module_2.py

from module_1 import calc_total

def calc_markup(total):
    return total * 0.1

def make_sale(items):
    total_price = calc_total(items)
    ...

While this is a contrived example, you can see that module_1 imports something from module_2, and module_2 imports something from module_1. If you tried to run a program containing these two modules, you would see the ...

Get Modular Programming with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.