Module question
Gary Herron
gherron at islandtraining.com
Tue Feb 21 18:38:17 EST 2006
Tuvas wrote:
>Could I just do this then?
>
>from foo import x?
>
>
Yes, you can do it that way. (f you have your modules importing each
other in a circular fashion, then this can cause trouble as x may not be
defined yet, so best to avoid that case.)
>One more question now that I've tried this. In my main function, I have
>alot of init code. I don't want this code to be re-ran when the second
>module imports the first. Is there any way around this? Thanks!
>
>
>
It won't be run twice. Module import is a two phase thing:
1. Read/compile the module and run it, creating a module object. (This
is done only once -- the first time a module is imported.)
2. Bind values in the importing code to the module or its attributes.
(This is done every time a module is imported.)
So... Your init code will be executed once -- in phase 1, no matter how
many times an import causes phase two to be performed.
Gary Herron
More information about the Python-list
mailing list