473,657 Members | 2,507 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 2076
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
2448
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 to Oracle databse? 3.Do I need Sybase database to migrate data to Oracle Database? 4.When i use import utility , will oracle create all the tables? If the above questions are not clear, pl give as much info as you can... because that's how the...
5
1552
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
3131
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 0 in j, and then increment i twice. On the next line, we start with i = 2. I is incremented twice so that it now equals 4.
8
5241
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 developing a web-based application, one part of which involves allowing the user the ability to page through transaction "history" information. The _summary_ history table will have the following fields: ServiceName, Date, User-Ref1, User-Ref2,...
4
3377
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. (Also same for SQL Server & VB) Disclaimer: Anyone who supplies me with some questions and applies for the job will get a different set of questions in the interview :-) --
162
14814
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 choice questions.etc.. I'll be indebted to everyone.. Thanks in advance.. regards vasant shetty Bangalore
4
3960
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 to file. I have 7 (and more coming) different fixed width files that my clients provide to me, within each one the field sizes and ordering is slightly different...maddening I know. Each file could possibly be as large as 150 MG with around...
4
10313
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 record is embeded by a tag which is the table name.
0
1245
by: bruce | last post by:
valid point...!! here's the test python.. ugly as it is!! Lodge It New All About ? Paste #83093
0
8845
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8743
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8622
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1736
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.