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

Discussion in 'Python' started by Peng Yu, Oct 15, 2009.

  1. Peng Yu

    Peng Yu Guest

    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?
    Peng Yu, Oct 15, 2009
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leslaw Bieniasz

    templates and cyclic dependencies

    Leslaw Bieniasz, Sep 20, 2004, in forum: C++
    Replies:
    3
    Views:
    400
    Victor Bazarov
    Sep 22, 2004
  2. xiaohuamao

    Cyclic dependencies

    xiaohuamao, Aug 9, 2006, in forum: C++
    Replies:
    1
    Views:
    392
    Pierre Barbier de Reuille
    Aug 9, 2006
  3. Craig Sanders

    Cyclic dependencies and namespaces.

    Craig Sanders, Sep 5, 2007, in forum: C++
    Replies:
    1
    Views:
    401
    Glen Dayton
    Sep 5, 2007
  4. Replies:
    2
    Views:
    474
  5. Jack
    Replies:
    6
    Views:
    346
    Patricia Shanahan
    Feb 9, 2008
Loading...

Share This Page