473,508 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Questions about the import ordering

ChrisWang
9 New Member
Dear all,

I am reading the book "Core Python Programming". In the chapter talking about modules, it says the modules should follow this ordering:

Expand|Select|Wrap|Line Numbers
  1. import Python Standard Library modules
  2.  
  3. import Python third party modules
  4.  
  5. import Application-specific modules
I am not sure whether the ordering will impact the module's loading or search efficiency. For example, a common module in the standard library can be free from being imported several times, if this module will be imported by application-specific modules.

test.py
Expand|Select|Wrap|Line Numbers
  1. #test.py
  2. import os
  3. import MyModuleA
  4. import MyModuleB
  5. ...
  6.  
MyModuleA.py
Expand|Select|Wrap|Line Numbers
  1. #MyModuleA.py
  2. import os
  3. ...
  4.  
MyModuleB.py
Expand|Select|Wrap|Line Numbers
  1. #MyModuleB.py
  2. import os
  3. ...
  4.  
If we follow the ordering to import the modules, here we import standard library "os", we may save some time, right? It's only my assumption and I am not sure I am right. But I am curious to know whether there are some benefits we can get from the this ordering.

Could anyone be kind enough to give me the answer? Thanks a lot.

Have a nice holiday!
Dec 18 '08 #1
4 2070
bvdet
2,851 Recognized Expert Moderator Specialist
A module is imported and executed only once. A subsequent import of the same module only creates a reference to the module namespace created on the previous import.

The ordering of the imports is primarily for consistency and is recommended in PEP 8. I don't know if there are any performance benefits.

-BV
Dec 18 '08 #2
ChrisWang
9 New Member
@bvdet
Thanks BV. Could you please show me detailed explanation about the consistency?
I've read the description about imports in PEP08, but I think I am still puzzled about that.
Dec 18 '08 #3
bvdet
2,851 Recognized Expert Moderator Specialist
ChrisWang,

Sorry, I have no detailed explanation. I think the reference to consistency is referring to Python applications in general.

If a top level module imports a required Python library module and another application specific module is imported and executed and it imports the same Python library module, a reference is made to the module instead of it being importing again. If you think about it, it makes sense to order the imports that way.

I would not import a module unless the current module requires it.

-BV
Dec 19 '08 #4
ChrisWang
9 New Member
Ok. Thank you very much.

Have a great holiday!
Dec 22 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2435
by: vv | last post by:
Hi All, I'm a newbie in databases..and I'm looking for answers for a few questions... Any help is appreciated! 1.What do you mean by importing a databse? 2.Is it possible to import Sybase databse...
5
1544
by: Harold Howe | last post by:
Howdy, 1- Are property reads in effect volatile? Or is the compiler allowed to optimize property reads away? ie class foo { public int bar { get { return m_bar;}
43
3089
by: Ook | last post by:
I had a coworker present this problem: int i = 0; int j,k; j = i++ + i++; k = ++i + ++i; And asked what j and k will have. I would expect that the compiler would add i to i (0+0), store the...
8
5208
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is...
4
3363
by: Trevor Best | last post by:
Does anyone know of a source of some good exam (pop quiz) type questions on Access? Looking to test candidates in a job interview. Pref a mix of multiple choice and ones that have real answers. ...
162
14706
by: techievasant | last post by:
hello everyone, Iam vasant from India.. I have a test+interview on C /C++ in the coming month so plz help me by giving some resources of FAQS, interview questions, tracky questions, multiple...
4
3953
by: BostonNole | last post by:
I am looking for suggestions on the most efficient way to import 7 different fixed width files into a DataSet. Not all at the same time. One file at a time, but the format could change from file...
4
10297
by: Alvin SIU | last post by:
Hi all, I have 6 tables inside a MS Access 2003 mdb file. I want to convert them as DB2 version -8 tables in AIX 5.2. I have exported them as 6 XML files. The XML files look fine. Each...
0
1233
by: bruce | last post by:
valid point...!! here's the test python.. ugly as it is!! Lodge It New All About ? Paste #83093
0
7226
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
7125
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
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
7388
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
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3199
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.