multiple import of a load of variables

Fuzzyman fuzzyman at gmail.com
Wed Mar 16 06:15:56 EST 2005


Torsten Bronger wrote:
> Hallöchen!
>
> "Fuzzyman" <fuzzyman at gmail.com> writes:
>
> > [...]
> >
> > I'm not entirely clear what you are trying to do
>
> The following: "variables.py" looks like this
>
> a = 1
> b = 2
>
> Then I have helper_a.py, helper_b.py, and helper_c.py which begin
> with
>
> from variables import *
>
> And finally, my_module.py starts with
>
> from variables import *
> from helper_a.py import *
> from helper_c.py import *
> from helper_c.py import *
>
> Now imagine that variables.py contained not only two but hundreds of
> variables.  Is this then still the most effective approach?
>

Hello Torsten,

This looks like the most effective approach to me. The additional cost
of the extra import statements would be very low. The alternative would
be to parse a config file and pass a data structure (containing all the
variables) between your modules. I can't imagine there is less overhead
in this. Either that or refactor so you only use the variables in one
place.

> > *but* - if you import the same module in several places (per
> > interpreter instance of course) the import will only be done
> > *once*. The other import statments just make that namespace
> > available from the namespace that does the import.
>
> Even if I use "from"?
>

Yes - all you do with the 'from' approach is get a direct reference to
that value, it still exists in it's original namespace.

import module
value = module.value
and :
from module import value

are exactly equivalent. The only difference is that the first construct
makes the whole of the 'module' namespace available as well - it no
less exists with the second construct though... you're just not keeping
a reference to it directly.

Not using the 'from' construct requires a bit of extra typing - but is
more explicit as to where the variables are coming from. This might
make your code a bit more readable.

Regards,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

> Tschö,
> Torsten.
> 
> -- 
> Torsten Bronger, aquisgrana, europa vetus




More information about the Python-list mailing list