importing with * (asterisk) versus as a namespace in python
Question: I know that its bad form to use `import *` in python, and I don't plan to make
a habit of it. However I recently came across some curious behaviour that I
don't understand, and wondered if someone could explain it to me.

Lets say I have three python scripts. The first, `first_script.py`, comprises:

    
    
    MESSAGE = 'this is from the first script'
    
    def print_message():
        print MESSAGE
    
    if __name__ == '__main__':
        print_message()
    

Obviously running this script gives me the contents of MESSAGE. I have a
second script called `second_script.py`, comprising:

    
    
    import first_script
    
    first_script.MESSAGE = 'this is from the second script'
    
    if __name__ == '__main__':
        first_script.print_message()
    

The behaviour (prints `this is from the second script`) makes sense to me.
I've imported `first_script.py`, but overwritten a variable within its
namespace, so when I call `print_message()` I get the new contents of that
variable.

However, I also have `third_script.py`, comprising:

    
    
    from first_script import *
    
    MESSAGE = 'this is from the third script'
    
    if __name__ == '__main__':
        print MESSAGE
        print_message()
    

This first line this produces is understandable, but the second doesn't make
sense to me. My intuition was that because I've imported into my main
namespace via * in the first line, I have a global variable called `MESSAGES`.
Then in the second line I overwrite `MESSAGES`. Why then does the function
(imported from the first script) produce the OLD output, especially given the
output of `second_script.py`. Any ideas?


Answer: This has to do with `Scope`. For a very excellent description of this, please
see [Short Description of Python Scoping
Rules](http://stackoverflow.com/questions/291978/short-description-of-python-
scoping-rules)

For a detailed breakdown with tons of examples, see
<http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/tutorials/scope_resolution_legb_rule.ipynb>

_Here's the details on your specific case:_

The `print_message` function being called from your third test file is being
asked to print out some `MESSAGE` object. This function will use the standard
`LEGB` resolution order to identify which object this refers to. `LEGB` refers
to `Local, Enclosing function locals, Global, Builtins`.

  1. Local - Here, there is no `MESSAGES` defined within the `print_message` function.
  2. Enclosing function locals - There are no functions wrapping this function, so this is skipped.
  3. Global - Any explicitly declared variables in the outer code. It finds `MESSAGE` defined in the global scope of the `first_script` module. _Resolution then stops, but i'll include the others for completeness._
  4. Built-ins - The list of python built-ins, [found here](https://docs.python.org/2/library/functions.html#id).

So, you can see that resolution of the variable `MESSAGE` will cease
immediately in `Global`, since there was something defined there.

Another resource that was pointed out to me for this is [Lexical scope vs
Dynamic
scope](http://en.wikipedia.org/wiki/Scope_%28computer_science%29#Lexical_scoping_vs._dynamic_scoping),
which may help you understand scope better.

HTH

