reload/import module bug?

David J. Fish djfish at sd.aetc.com
Mon Sep 13 20:22:21 EDT 1999


   I have recently discovered a feature in Python which does not seem to
make
a whole lot of sense.  Here is the sequence of what I do and what
happens.  If someone could explain what Python is doing and/or why it is
doing it, I'd greatly appreciate it.  Thanks.

First I create a module file, called, knights.py

In this file, I create a function called printer:
def printer(x):
   print(x)

>From the prompt
% python
Python 1.5.2 (#1, Apr 18 1999, 16:03:16)  [GCC pgcc-2.91.60 19981201
(egcs-1.1.1  on linux2

>>> import knights
>>> knights.printer('nih!')
nih!

Now, back in my knights module file, I delete the printer function and
write a
new function
def echoer(x):
   print(x*4)

Then, back at the interactive prompt:
>>> del(knights)
>>> import knights
>>> reload(knights)
<module 'knights' from 'knights.py'>
>>> knights.echoer('nih!')
nih!nih!nih!nih!
>>> knights.printer('nih!)
nih!
>>> dir(knights)
['__builtins__', '__doc__', '__file__', '__name__', 'echoer', 'printer']

-- 
David J. Fish




More information about the Python-list mailing list