![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Dec 2006
Posts: 53
Rep Power: 0
![]() |
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.
|
|
|
|
|
|
#3 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
python Syntax (Toggle Plain Text)
|
|
|
|
|
|
|
#4 |
|
Expert Programmer
|
|
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
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.
|
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Java CS Project | Watts | Existing Project Development | 12 | Jun 20th, 2006 3:39 PM |
| C++ OWL What am I doing wrong | Vagabond | C++ | 7 | Mar 24th, 2006 5:31 PM |
| method doesn't recognize variable | Krista | Java | 1 | Dec 5th, 2005 5:40 PM |
| converting from javax.swing.JTextField to java.lang.String | Krista | Java | 5 | Dec 5th, 2005 4:08 PM |
| cgi module index.py?var=x how do i retrieve this w/o os module | cypherkronis | Python | 1 | Jul 3rd, 2005 1:10 PM |