[Tutor] Re: importing type

Doug Stanfield DOUGS@oceanic.com
Mon, 5 Mar 2001 08:00:30 -1000


[Alan Gauld wrote:]

>> This works best if you are checkjing for type several 
>> times since the import overhead only gets called once.

>>>     type(mystr) == type("") 

>> This is better if you only do it once since the extra 
>> function call is probably faster than importing the 
>> type module.

[Stevenson M Hickey asked:]
> Am I misunderstanding something?

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

No, as I understand it, the import function reads the file and does a
byte-compile to turn it in to Python's op-code representation.  It then
stores it in the same way it does the program it was called from, except...

> 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.

.. it needs to be able to distinguish it from other imported or main module
functions.  The 'type.' in this case is just a reference like almost
everything else in Python.

> 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!

I don't happen to have IDLE set up right now so I can't directly answer this
question.  Notwithstanding that Windows is as real world as it gets, you can
explore.  Use the 'dir()' function in each environment and compare the
results.  When I do it from a direct interpreter session on Windows it looks
like this:

D:\PYTHON>python
ActivePython 2.0, build 202 (ActiveState Tool Corp.)
based on Python 2.0 (#8, Oct 19 2000, 11:30:05) [MSC 32 bit (Intel)] on
win32
Type "copyright", "credits" or "license" for more information.
>>> dir()
['__builtins__', '__doc__', '__name__']
>>> dir("__builtins_")
['capitalize', 'center', 'count', 'encode', 'endswith', 'expandtabs',
'find', 'i
ndex', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle',
'isuppe
r', 'join', 'ljust', 'lower', 'lstrip', 'replace', 'rfind', 'rindex',
'rjust', '
rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title',
'tra
nslate', 'upper']
>>>

It doesn't looke like 'type' is there.  Maybe in the IDLE environment it is
already imported to make IDLE work.  Since there seems to be a difference,
I'd suggest getting in the habit of importing what you need just for
portability sake.

> So what's the Overhead for this?

Ultimately the question comes down to, does my program run fast enough.  In
all that I've ever done with Python it has never been slow enough for me to
be concerned about optimizing out such things as imports.

> If you enter 'from type import *'  then, I think, you would
> have the overhead of each function in type.py being taken into Ram.  

I think it is more or less the same with the exception that now you lose the
name referenceing ability.  The above is generally considered _BAD_ in a
Python module.  The exception is libraries like Tkinter that are designed
for it.  About the only time you should be using the import star form is in
an interpretive session where you are exploring a libraries functions.

-Doug-