import order or cross import

Roland Hedberg roland.hedberg at
Wed Jan 3 09:29:01 CET 2007


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:

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

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):
    def __str__(self):
        return self.color

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

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

import ONE

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

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


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

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

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

-- Roland

More information about the Python-list mailing list