473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

multiple import of a load of variables

Hallöchen!

I have a file that looks a little bit like a C header file with a
long list of variables (actually constants) definitions, e.g.

VI_ATTR_TIMO = 0x54378
....

Actually I need this in a couple of low-level modules that are
imported into the main module, and in the main module itself. They
may be also used in the programs that import the main module.

Probably it doesn't mean a significant loss of performance anyway
since all of it is done only at startup, but I wonder whether it's
better to keep everything that depends on this "header" file
together so that it must be looked over by Python only once. Is
this correct, or is there some sort of implicit optimisation that
makes both variants almost equivalent?

Tschö,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus
Jul 18 '05 #1
4 1599

Torsten Bronger wrote:
Hallöchen!

I have a file that looks a little bit like a C header file with a
long list of variables (actually constants) definitions, e.g.

VI_ATTR_TIMO = 0x54378
...

Actually I need this in a couple of low-level modules that are
imported into the main module, and in the main module itself. They
may be also used in the programs that import the main module.

Probably it doesn't mean a significant loss of performance anyway
since all of it is done only at startup, but I wonder whether it's
better to keep everything that depends on this "header" file
together so that it must be looked over by Python only once. Is
this correct, or is there some sort of implicit optimisation that
makes both variants almost equivalent?

I'm not entirely clear what you are trying to do *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.

This means there is little extra overhead for importing a module that
has already been imported elsewhere.

HTH ?

Regards,

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

Tschö,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus


Jul 18 '05 #2
Hallöchen!

"Fuzzyman" <fu******@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?
*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"?

Tschö,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus
Jul 18 '05 #3

Torsten Bronger wrote:
Hallöchen!

"Fuzzyman" <fu******@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


Jul 18 '05 #4
Torsten Bronger wrote:
Hallöchen!

"Fuzzyman" <fu******@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?


Ugh. I really don't like the 'from module import *' approach in any
case, but here, you're binding each name from variables.py at least
seven times. Now, it's not like binding names is all *that*
expensive, but still... I'd call this a code smell.

You say that users of my_module.py may need direct access to these
variables; to do that well, it does make some sense that my_module
should import * from variables. However, I doubt there's any good
justification for importing the helper modules that way, nor for
importing * from variables in each of the helper modules.

If you use normal imports in those cases, then once variables.py has
been loaded into sys.modules (i.e. imported for the first time), you
create n+3 name bindings as opposed to the 7*n bindings your method is
creating, where n is the number of names in variables.py. (The '+6'
is the 'variables' name in each of the helper modules.)

More importantly, you get clearer code that's easier to maintain and
troubleshoot. This fact is far more important than the cost of
creating name bindings, or any other form of 'efficiency' that is
likely to apply. As far as I'm concerned, if you're just going to
'import *' your helper modules, you might as well leave the whole mess
as one big file, because you're throwing away almost all of the
benefits of dividing it into modules.

Jeff Shannon

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: flupke | last post by:
Hi, i need to develop a gui which will load several "windows" depending on what the users selects in the menu and i thought i could accomplish this with panels. What i'm trying to do to test...
4
by: MackS | last post by:
Hi I'm new to Python, I've read the FAQ but still can't get the following simple example working: # file main_mod.py: global_string = 'abc' def main():
11
by: Ohaya | last post by:
Hi, I'm trying to understand a situation where ASP seems to be "blocking" of "queuing" requests. This is on a Win2K Advanced Server, with IIS5. I've seen some posts (e.g.,...
8
by: DB2 Novice | last post by:
I am trying to use DB2 Control Centre (version 8.2) to load one flat file into multiple tables. However, I don't see the options in Control Centre that allows that. Anyone knows how to do this?...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
4
by: platinumplus000 | last post by:
Does any XSLT expert know if there is a way to import xml data as a parameter into an XSL stylesheet. I know you can import an XML file using the document('file.xml') function but the data I get...
1
by: baling | last post by:
Hi.... Hi everybody, i have a code that i make in VBA and know I want to use this code in to VB6. But i don't know how to use that code in to VB 6.0 Please correct this code so i can use it in VB...
16
by: devicerandom | last post by:
Hi, I am currently using the Cmd module for a mixed cli+gui application. I am starting to refactor my code and it would be highly desirable if many commands could be built as simple plugins. ...
11
by: Phil Latio | last post by:
I'm designing a small framework with a lot of classes (1 file per class) and want to know the best method of calling each class? Obviously I could I call each file that is used but that could be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.