Why is module global assignment different simple types vs classes/dictionaries?
Question: Given the following example code:

test.py

    
    
    import module
    
    print 'main: Vars.foo: %s' % (module.Vars.foo)
    print 'main: d.foo: %s' % (module.d['foo'])
    print 'main: foo: %s' % (module.foo)
    print
    module.Vars.foo = 2
    module.d['foo']=2
    module.foo = 2
    
    def baz():
        print 'baz: Vars.foo: %s' % (module.Vars.foo)
        print 'baz: d.foo: %s' % (module.d['foo'])
        print 'baz: foo: %s' % (module.foo)
        print
        module.bar()
    
    if __name__ == '__main__':
        baz()
    

module/  
    module.py  
    `__init__.py`

`__init__.py`

    
    
    from module import *
    

module.py

    
    
    class Vars:
        foo = None
    d = {'foo': None}
    foo = None
    
    def bar():
        print 'bar: Vars.foo: %s' % (Vars.foo)
        print 'bar: d.foo: %s' % (d['foo'])
        print 'bar: foo: %s' % (foo)
    

produces this output when test.py is run:

    
    
    main: Vars.foo: None
    main: d.foo: None
    main: foo: None
    
    baz: Vars.foo: 2
    baz: d.foo: 2
    baz: foo: 2
    
    bar: Vars.foo: 2
    bar: d.foo: 2
    bar: foo: None
    

All of this makes sense to me except the bar() output. Why is the bar() output
different for a class/dictionary vs. a simple type? I think it has something
to do with the mutability of a class/dictionary but I'm not sure. What I'm
specifically looking for is the actual python language rules that are causing
that bar output to be as it is.


Answer: In test.py you are importing the `module` _package_ , not the `module.module`
_module_. In this package's `__init__.py` you're doing a star import from the
`module.module` module. So when you rebind `module.foo` in test.py, it's the
package's 'foo' you are rebinding, not the module's one. Wrt/ the other
objects, you are mutating them, not rebinding them, which is a different
operation.

If in test.py you replace `import module` with `from module import module`
you'll get a different behaviour.

