Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 28th, 2006, 1:25 PM   #1
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Import a Module

I usually import all the needed modules in the beginning of my program. I keep seeing code that imports modules within a function, wouldn't that be bad? I assume that the module will be imported every time the function is called.
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Dec 28th, 2006, 1:52 PM   #2
BinarySurfer
Programmer
 
BinarySurfer's Avatar
 
Join Date: Dec 2006
Posts: 53
Rep Power: 0 BinarySurfer is an unknown quantity at this point
I've done it before without any problems, though I would avoid importing the randomizer in a function. The module would be loaded at the beginning of the function (or where ever) and dropped at the end, so it's I think it's okey. I don't recommend doing it that way because it hurts performance if that function is used often. I would do it in a function that's not used often, otherwise I would import it at the beginning of the code like usual.
BinarySurfer is offline   Reply With Quote
Old Dec 28th, 2006, 2:41 PM   #3
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Dietrich View Post
I usually import all the needed modules in the beginning of my program. I keep seeing code that imports modules within a function, wouldn't that be bad? I assume that the module will be imported every time the function is called.
Modules are cached in memory, so they are only ever imported once. This can be tested via the following code:
python Syntax (Toggle Plain Text)
  1. def foo():
  2. import string
  3. string.test = "Hello"
  4. print string.test
  5.  
  6. def bar():
  7. import string
  8. print string.test
  9.  
  10. foo()
  11. bar()
However, even though modules are cached, there's still some overhead in searching the cache for the module. So although it doesn't make much difference, it still results in slight performance degradation
Arevos is offline   Reply With Quote
Old Dec 28th, 2006, 6:10 PM   #4
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 909
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Quote:
Originally Posted by Arevos View Post
However, even though modules are cached, there's still some overhead in searching the cache for the module. So although it doesn't make much difference, it still results in slight performance degradation
Wouldn't the cache have to be searched either way?
titaniumdecoy is offline   Reply With Quote
Old Dec 29th, 2006, 3:21 AM   #5
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
It will only get searched once you import the module at file scope. It will be searched every time you run the function if you import inside the function.
Game_Ender is offline   Reply With Quote
Old Dec 29th, 2006, 6:47 AM   #6
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by titaniumdecoy View Post
Wouldn't the cache have to be searched either way?
As I understand it, if it's already imported, only the namespace must be searched for the module name. If it's imported in the middle of the function, first the module cache has to be searched (in order to import the module into the current namespace), and then the namespace.
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Java CS Project Watts Existing Project Development 12 Jun 20th, 2006 4:39 PM
C++ OWL What am I doing wrong Vagabond C++ 7 Mar 24th, 2006 6:31 PM
method doesn't recognize variable Krista Java 1 Dec 5th, 2005 6:40 PM
converting from javax.swing.JTextField to java.lang.String Krista Java 5 Dec 5th, 2005 5:08 PM
cgi module index.py?var=x how do i retrieve this w/o os module cypherkronis Python 1 Jul 3rd, 2005 2:10 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:28 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC