Problem with reimporting modules

Christoph Zwerschke cito at online.de
Sun Feb 11 06:53:58 EST 2007


I'm currently investigating a problem that can hit you in TurboGears 
when Kid template modules are reloaded in the background, because in 
certain situations, global variables suddenly are set to None values.

I tracked it down to the following behavior of Python. Assume you have a 
module hello.py like that:

---- hello. py ----
greeting = 'Hello!'
def print_hello():
     print greeting
-------------------

Now run the following code:

from hello import print_hello
print_hello()
import sys
del sys.modules['hello'] # delete module
import hello # recreate module
print_hello()

The second print_hello() prints "None" instead of "Hello!". Why is that? 
I had expected that it either prints an error or print "Hello!" as well.

Is this intended behavior of Python?

-- Christoph



More information about the Python-list mailing list