[Tutor] Re: Tutor digest, Vol 1 #634 - 13 msgs

D-Man dsh8290@rit.edu
Mon, 5 Mar 2001 17:59:35 -0500


FYI,

[ when replying to the digest, it is good to change the subject to
something meaningful,  otherwise some people (like me) have a tendency
to kill-thread it without reading it ]

[ also, plain-text is better than HTML mail,  this was wrapped
horridly and I had to sort through this to find what you wrote as
opposed to what you replied to ]


On Mon, Mar 05, 2001 at 08:20:15AM -0800, Stevenson M Hickey wrote:

|    Am I misunderstanding something? If you enter:
|    "import type" and hit return, are you not adding only a pointer to the
|    file 'type.py'? 

The import statement locates the file.  It then parses and
byte-compiles the file (unless it has already been byte-compiled).  It
then executes any file-level statements in the file.  The module is
loaded into memory at this time.   Also, imports only actually happen
once.  If a module is already loaded, the import simply creates a name
in the current namespace that refers to the already loaded module.

|    I ask this, because, after having written 'import
|    type', you have to instantiate a function in type.py by using the
|    referrent preface -- 'type.' in front of the function name, before you
|    can use the function. 

You didn't instantiate the function then,  it was instantiated when
the import reached the "def" statement.  Prefixing module members with
the module name simply tells the interpreter which module the name you
want should be found in.  Take the following two files/modules for
example :


# foo.py
def func() :
    print "This is foo"


# bar.py
def func() :
    print "This is bar"


Now try to use these modules :

import foo
import bar

func()  # which func??
        # actually, there is no func in the current namespace so this
        # is a NameError

foo.func()  # prints "This is foo"
bar.func()  # prints "This is bar"



If you were to use the evil from import * syntax, you would have
problems :


from foo import *
from bar import *

func() # prints "This is bar"
# now I can't get "This is foo" since I overwrote the reference

|    Secondly, when using IDLE, I noticed that I can
|    use the function 'type' without an 'import type' command.  Is this
|    something in IDLE, or is 'type' a preferred function that is contained
|    inside the Python executable?  Of course, I am working on Windows
|    (yech) and so do not know what the 'REAL WORLD' viewpoint is! So

The function type() is in a builtin function.  It is in the module
__builtins__.  You don't do anything to import this module.

The types module is different.  It is separate, and must be imported
in order to use it.  Also, the 'types' module doesn't define any
functions, just constants that can be useful.


|    what's the Overhead for this? If you enter 'from type import *'  then,
|    I think, you would have the overhead of each function in type.py being
|    taken into Ram.   

Overhead isn't something to worry about unless your program is too
slow.  from types import *  is considered bad (actually, any from
import *) because of the namespace clashing issue as I demonstrated
above.  Each function is loaded into RAM when the module is imported
regardless of whether a "normal" or a from-* import is performed.

The from-* form is slower only because the interpreter must iterate
over each name in the module and insert it into the current module.
The extra memory usage is only slightly higher than 4 bytes for each
name (not very significant) (a pointer, plus the other fields in a
PyObject).

-D