Python 2.7 : global with imported modules
Question: In Python 2.7, depending on how I import a module, global variables can become
unreachable.

I have a file test.py which contains the following:

    
    
    x = None
    
    def f():
        global x
        x = "hello"
        print x
    

I get the following expected behavior:

    
    
    >>> import test
    >>> print test.x
    None
    >>> test.f()
    hello
    >>> print test.x
    hello
    

But now if i do an ugly 'import *' instead, i get the follwoing:

    
    
    >>> from test import *
    >>> print x
    None
    >>> f()
    >>> print x
    None
    

So variable x is no longer accessible.. Any clues ?

thanks, y


Answer: `from test import *` will do the equivalent of `x = test.x`. If you change
`test.x` later (which your call to `foo()` will do), it will not change the
copy of x in your local namespace.

