import order or cross import

Discussion in 'Python' started by Roland Hedberg, Jan 3, 2007.

  1. Hi!

    I'm having a bit of a problem with import.

    I'm writing a marshalling system that based on a specification will
    create one or more files containing mostly class definitions.

    If there are more than one file created (and there are reasons for
    creating more than one file in some instances) then they will import
    each other since there may be or is interdependencies in between them.

    And this is where the going gets tough.

    Let's assume I have the following files:

    ------------- ONE.py --------------------

    import TWO

    class Car:
    def __init__(self):
    self.color = None

    def set_color(self,v):
    if isinstance(v,TWO.Black) or isinstance(v,TWO.White):
    self.color = v

    class BaseColor:
    def __init__(self):
    pass
    def __str__(self):
    return self.color

    if __name__ == "__main__":
    car = Car()
    color = TWO.Black()
    car.set_color(color)
    print car.color

    -------------- TWO.py -------------------

    import ONE

    class Black(ONE.BaseColor):
    color = "Black"
    def __init__(self):
    ONE.BaseColor.__init__(self)

    class White(ONE.BaseColor):
    color = "White"
    def __init__(self):
    ONE.BaseColor.__init__(self)

    -----------------------------------------

    Now, running ONE.py causes no problem it will print "Black", but running
    TWO.py I get:
    AttributeError: 'module' object has no attribute 'BaseColor'

    So, how can this be solved if it can be ?

    To join ONE.py and TWO.py into one file could be a solution if there
    where no other conditions (non-language based) that prevented it.

    -- Roland
    Roland Hedberg, Jan 3, 2007
    #1
    1. Advertising

  2. Roland Hedberg kirjoitti:
    > Hi!
    >
    > I'm having a bit of a problem with import.
    >
    > I'm writing a marshalling system that based on a specification will
    > create one or more files containing mostly class definitions.
    >
    > If there are more than one file created (and there are reasons for
    > creating more than one file in some instances) then they will import
    > each other since there may be or is interdependencies in between them.
    >
    > And this is where the going gets tough.
    >
    > Let's assume I have the following files:
    >
    > ------------- ONE.py --------------------
    >
    > import TWO
    >
    > class Car:
    > def __init__(self):
    > self.color = None
    >
    > def set_color(self,v):
    > if isinstance(v,TWO.Black) or isinstance(v,TWO.White):
    > self.color = v
    >
    > class BaseColor:
    > def __init__(self):
    > pass
    > def __str__(self):
    > return self.color
    >
    > if __name__ == "__main__":
    > car = Car()
    > color = TWO.Black()
    > car.set_color(color)
    > print car.color
    >
    > -------------- TWO.py -------------------
    >
    > import ONE
    >
    > class Black(ONE.BaseColor):
    > color = "Black"
    > def __init__(self):
    > ONE.BaseColor.__init__(self)
    >
    > class White(ONE.BaseColor):
    > color = "White"
    > def __init__(self):
    > ONE.BaseColor.__init__(self)
    >
    > -----------------------------------------
    >
    > Now, running ONE.py causes no problem it will print "Black", but running
    > TWO.py I get:
    > AttributeError: 'module' object has no attribute 'BaseColor'
    >
    > So, how can this be solved if it can be ?
    >
    > To join ONE.py and TWO.py into one file could be a solution if there
    > where no other conditions (non-language based) that prevented it.
    >
    > -- Roland
    >


    Maybe I'm missing something, but why is the class BaseColor in file
    ONE.py and not in TWO.py?

    Cheers,
    Jussi
    Jussi Salmela, Jan 3, 2007
    #2
    1. Advertising

  3. Roland Hedberg

    Duncan Booth Guest

    Roland Hedberg <> wrote:

    > Now, running ONE.py causes no problem it will print "Black", but running
    > TWO.py I get:
    > AttributeError: 'module' object has no attribute 'BaseColor'
    >
    > So, how can this be solved if it can be ?
    >


    When you execute an import statement, it checks whether the module already
    exists. If it does exist then it immediately returns the module object. If
    the module does not yet exist then the module is loaded and the code it
    contains is executed.

    Remember that all statements in Python are executed at the time they are
    encountered: there are no declarations (apart from 'global') so no looking
    ahead to see what classes or functions are coming up.

    One other complication in your particular instance: when you run a ONE.py
    as a script the script is loaded in a module called '__main__', so 'import
    ONE' will actually load a separate module which is NOT the same as the
    __main__ module. Likewise when you run TWO.py as a script you have three
    modules __main__ (loaded from TWO.py), ONE, and TWO.

    So running TWO.py, you get:

    import ONE
    --- loads the module ONE and starts executing it
    import TWO
    --- loads the module TWO and starts executing it
    import ONE
    --- the module ONE already exists
    (even though so far it hasn't got beyond
    the import TWO line) so the empty module is returned.
    class Black(ONE.BaseColor):
    --- ONE doesn't have a BaseColor attribute yet so you get
    an error.

    The fix in this sort of case is usually to extract the base class out to a
    third module. If you put BaseColor in base.py then you can safely import
    that anywhere you want it.

    An alternative in this case would be to edit ONE.py and move the line
    'import TWO' down below the definition of BaseColor: nothing before that
    actually requires TWO to have been imported yet.

    However, you really should try to separate scripts from modules otherwise
    the double use as both __main__ and a named module is going to come back
    and bite you.
    Duncan Booth, Jan 3, 2007
    #3
  4. Duncan Booth wrote:

    >
    > Remember that all statements in Python are executed at the time they are
    > encountered: there are no declarations (apart from 'global') so no looking
    > ahead to see what classes or functions are coming up.


    Yes, I've seen this time and time again.

    > One other complication in your particular instance: when you run a ONE.py
    > as a script the script is loaded in a module called '__main__', so 'import
    > ONE' will actually load a separate module which is NOT the same as the
    > __main__ module. Likewise when you run TWO.py as a script you have three
    > modules __main__ (loaded from TWO.py), ONE, and TWO.


    Hmm, that's interesting.

    >
    > The fix in this sort of case is usually to extract the base class out to a
    > third module. If you put BaseColor in base.py then you can safely import
    > that anywhere you want it.


    OK, so I'll go for this option. Takes some more intelligence in the
    marshalling code but it should be doable.

    > An alternative in this case would be to edit ONE.py and move the line
    > 'import TWO' down below the definition of BaseColor: nothing before that
    > actually requires TWO to have been imported yet.


    Since no person is involved, in creating files like ONE.py and TWO.py,
    this also has to be done by the marshalling code.
    I've no clear idea which alternative would be best/easiest but your
    first one seems clearer.

    > However, you really should try to separate scripts from modules otherwise
    > the double use as both __main__ and a named module is going to come back
    > and bite you.


    Oh, that was only there as an example, so you could see the intent usage.

    Thanks Duncan!

    -- Roland
    Roland Hedberg, Jan 3, 2007
    #4
  5. Jussi Salmela wrote:
    > Roland Hedberg kirjoitti:


    >> I'm having a bit of a problem with import.
    >>
    >> I'm writing a marshalling system that based on a specification will
    >> create one or more files containing mostly class definitions.


    > Maybe I'm missing something, but why is the class BaseColor in file
    > ONE.py and not in TWO.py?


    This has to do with the how the specifications that the marshallings
    system works with are structured. And it is therefor not something I can
    control.

    -- Roland
    Roland Hedberg, Jan 3, 2007
    #5
  6. At Wednesday 3/1/2007 07:56, Duncan Booth wrote:

    >However, you really should try to separate scripts from modules otherwise
    >the double use as both __main__ and a named module is going to come back
    >and bite you.


    I second this. Something with a __main__ can use (import) other
    modules, but shoud not be used (imported) by anyone.


    --
    Gabriel Genellina
    Softlab SRL






    __________________________________________________
    Preguntá. Respondé. Descubrí.
    Todo lo que querías saber, y lo que ni imaginabas,
    está en Yahoo! Respuestas (Beta).
    ¡Probalo ya!
    http://www.yahoo.com.ar/respuestas
    Gabriel Genellina, Jan 3, 2007
    #6
    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. Soren Kuula
    Replies:
    2
    Views:
    487
    Soren Kuula
    Feb 1, 2004
  2. Joerg Sonnenberger

    Nested packages and import order bug

    Joerg Sonnenberger, Oct 6, 2003, in forum: Python
    Replies:
    0
    Views:
    299
    Joerg Sonnenberger
    Oct 6, 2003
  3. cspoh
    Replies:
    0
    Views:
    234
    cspoh
    Jul 31, 2003
  4. Stephan Kämper
    Replies:
    2
    Views:
    229
    Stephan Kämper
    Jan 18, 2004
  5. Erwin Moller
    Replies:
    9
    Views:
    124
    Clifford Heath
    May 8, 2008
Loading...

Share This Page