when is a != foo.a?

Chaz Ginger cginboston at hotmail.com
Mon Aug 28 07:53:28 EDT 2006


Duncan Booth wrote:
> Chaz Ginger wrote:
> 
>> Can someone explain what is really going on here?
> 
> Think of 'from x import y' as an assignment. Roughly equivalent to:
> 
>    y = sys.modules['x'].y
> 
> (except of course you don't have to have imported sys, and it will load the 
> module 'x' if it hasn't already been imported.)
> 
>> b.py ----------------------
>>
>> from a import foo
> 
> In other words:
> 
>    foo = a.foo
> 
> foo in module b is initialised from a.foo, but it is a separate variable. 
> So when a.foo is rebound that doesn't affect b.foo.
> 
>> def b(): print foo
>>
>> c.py ----------------------
>>
>> import a
>> from b import b
> 
> and here:
>    b = b.b
> 
>> print 'per a.a() ',a.foo
>> a.a(245)
>> print 'expect 245 ', a.foo
>> b()
>>
> 
> 

Thanks, Duncan. It now makes sense.




More information about the Python-list mailing list