import order or cross import
Jussi Salmela
tiedon_jano at hotmail.com
Wed Jan 3 04:59:59 EST 2007
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
More information about the Python-list
mailing list