How to organize code for conversion between different classes?(avoiding cyclic dependencies)

P

Peng Yu

Suppose I have classes 'A', 'B', 'C', 'D'. The definition of these
classes are long enough so that I have to put each class in a separate
module 'mA', 'mB', 'mC', 'mD', which are in packages 'pA', 'pB', 'pC',
'pD', respectively. And there were no need to have conversion
functions between these classes.

As my program evolves, I need to have a conversion function between
any pair of classes. I could define the conversion functions as member
methods like the following. But if I do this for the classes B, C, D,
I would end up with cyclic dependencies, which should be avoid.

################
import pB.mB
import pC.mC
import pD.mD

class A:
...
def convert_to_B():
....

def convert_to_C():
....

def convert_to_D():
....
################

Another choice is that I can put the conversion functions in some
modules and import 'mA', 'mB', 'mC', 'mD' when needed. However, since
mA, mB, mC, mD are in different packages, I'm not sure where to put
the new modules that have the conversion functions.

I'm wondering how this case shall be handled. Could somebody give some hint?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top