FW: Re: [Tutor] command line

dman dman@dman.ddts.net
Sat, 30 Mar 2002 17:37:25 -0600


I sent this privately because I missed the Cc: in the headers.  Here
it is for the benefit of the rest of you.

-D


----- Forwarded message from dman <dman@dman.ddts.net> -----
From: dman <dman@dman.ddts.net>
To: Erik Price <erikprice@mac.com>
Subject: Re: [Tutor] command line
Date: Sat, 30 Mar 2002 16:50:25 -0600

On Sat, Mar 30, 2002 at 05:02:04PM -0500, Erik Price wrote:
| 
| On Saturday, March 30, 2002, at 04:54  PM, dman wrote:
| 
| >If the script did
| >    import Stock
| >then you would have the name "Stock" in your namespace and it would
| >refer to the Stock module.
| >
| >If the script did (bad!)
| >    from Stock import *
| >then you would have all the names from Stock in your namespace.
| 
| Why is the second one bad?  Doesn't it achieve the same effect?  The two 
| are not the same?

No, they're not the same.  One of them creates lots of names in your
namespace and the other one doesn't.  The problem with that is
several-fold :
    1)  when you use a name, you can no longer immediately tell if it
        is a local variable, module-level variable, or something from
        a separate module

    2)  what if some (future) version of that module adds additional
        names to its namespace?  Now they are in yours as well, and
        may have a conflict with other names in your module

    3)  if the name in the module's namespace is rebound, your name
        isn't also rebound.

For a first example, here's a mistake quite a few newbies do :


#### newbie_mod

# this part is ok,
a_file = open( "data" , "r" )
print a_file
print a_file.read()

# oops
from os import *

# this doesn't mean what it seems to
another_file = open( "data" , "r" )
print another_file 
print another_file.read()



Here's another example that illustrates the other points :


######### mod1_good
import mod2

# note that the name is misleading, it really only lists the names in
# this module
print globals()
print mod2.foo
mod2.bar()
print mod2.foo


######### mod1_bad
from mod2 import *

print globals()
print mod2.foo
print foo
mod2.bar()
print mod2.foo
print foo


######### mod2

foo = "spam"
def bar() :
    global foo
    foo = "eggs"


-D

-- 

He who belongs to God hears what God says.  The reason you do not hear
is that you do not belong to God.
        John 8:47


----- End forwarded message -----

-- 

Through love and faithfulness sin is atoned for;
through the fear of the Lord a man avoids evil.
        Proverbs 16:6