Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I resolve a circular import of two Python classes in the same module?

I have two tightly-coupled Python classes that need references to each other (at the class, not instance, level). How can I resolve the circular imports? Ideally I'd like to be able to make it work either within the same module or between two distinct modules, but I'll settle for one or the other.

# yin_yang.py

class MyYin(Yin):
    __yang__ = MyYang

class MyYang(Yang):
    __yin__ = MyYin
like image 799
Sasgorilla Avatar asked Mar 04 '23 10:03

Sasgorilla


1 Answers

You could set the class attributes for one or both classes after they have been declared.

class MyYin(Yin):
    pass

class MyYang(Yang):
    __yin__ = MyYin

MyYin.__yang__ = MyYang
like image 174
Phillip Martin Avatar answered Apr 28 '23 01:04

Phillip Martin