[Tutor] (getch() equivalent) [fun with imports]

Bob Gailer ramrom@earthling.net
Sun Mar 9 08:52:06 2003


--=======5F577F08=======
Content-Type: multipart/alternative; x-avg-checked=avg-ok-382D5B7F; boundary="=====================_7987665==.ALT"


--=====================_7987665==.ALT
Content-Type: text/plain; x-avg-checked=avg-ok-382D5B7F; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 8bit

At 08:57 AM 3/9/2003 -0500, Erik Price wrote:
>On Sunday, March 9, 2003, at 02:11  AM, Danny Yoo wrote:
>>Just as local variables are visible only with the enclosing function,
>>local module imports are treated with the same rule.  Doing 'import'
>>within a function very unusual: it's not often useful to localize a module
>>to a single function, since it's probably going to be used everywhere in a
>>program.
>Is there a significant performance hit when importing a module twice in 
>this fashion?  Or does the Python processor keep all of the imported 
>objects cached in memory so that if it is imported a second time, even in 
>a separate namespace, a module will not be searched for and loaded again?

 From the Python Language Reference (always a good place to look, (along 
with the Library Reference) for questions like this):

"Import statements are executed in two steps: (1) find a module, and 
initialize it if necessary; (2) define a name or names in the local 
namespace (of the scope where the import statement occurs). The first form 
(without from) repeats these steps for each identifier in the list....

The system maintains a table of modules that have been initialized, indexed 
by module name. This table is accessible as sys.modules. When a module name 
is found in this table, step (1) is finished...."

HTH

Bob Gailer
mailto:ramrom@earthling.net
303 442 2625


--=====================_7987665==.ALT
Content-Type: text/html; x-avg-checked=avg-ok-382D5B7F; charset=us-ascii
Content-Transfer-Encoding: 8bit

<html>
<body>
At 08:57 AM 3/9/2003 -0500, Erik Price wrote:<br>
<blockquote type=cite class=cite cite>On Sunday, March 9, 2003, at
02:11&nbsp; AM, Danny Yoo wrote:<br>
<blockquote type=cite class=cite cite>Just as local variables are visible
only with the enclosing function,<br>
local module imports are treated with the same rule.&nbsp; Doing
'import'<br>
within a function very unusual: it's not often useful to localize a
module<br>
to a single function, since it's probably going to be used everywhere in
a<br>
program.</blockquote>Is there a significant performance hit when
importing a module twice in this fashion?&nbsp; Or does the Python
processor keep all of the imported objects cached in memory so that if it
is imported a second time, even in a separate namespace, a module will
not be searched for and loaded again?</blockquote><br>
 From the Python Language Reference (always a good place to look, (along
with the Library Reference) for questions like this):<br><br>
&quot;Import statements are executed in two steps: (1) find a module, and
initialize it if necessary; (2) define a name or names in the local
namespace (of the scope where the <tt>import</tt> statement occurs). The
first form (without <tt>from</tt>) repeats these steps for each
identifier in the list....<a name="l2h-356"></a>  <br><br>
The system maintains a table of modules that have been initialized,
indexed by module name. This table is accessible as sys.modules. When a
module name is found in this table, step (1) is
finished....&quot;<br><br>
HTH<br>
<x-sigsep><p></x-sigsep>
Bob Gailer<br>
<a href="mailto:ramrom@earthling.net" eudora="autourl">mailto:ramrom@earthling.net</a><br>
303 442 2625<br>
</body>
</html>


--=====================_7987665==.ALT--

--=======5F577F08=======
Content-Type: text/plain; charset=us-ascii; x-avg=cert; x-avg-checked=avg-ok-382D5B7F
Content-Disposition: inline


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.459 / Virus Database: 258 - Release Date: 2/25/2003

--=======5F577F08=======--