surprising behaviour of global dictionaries

Dave Angel d at davea.name
Tue Oct 9 12:10:40 EDT 2012


On 10/09/2012 11:36 AM, Michele Simionato wrote:
> On Tuesday, October 9, 2012 5:24:17 PM UTC+2, Peter Otten wrote:
>> Seriously, you shouldn't use the main script as a library; it is put into 
>>
>> the sys.modules cache under the "__main__" key. Subsequent imports under its 
>>
>> real name will not find that name in the cache and import another instance 
>>
>> of the module, with puzzling effects
> Actually I usually never use the main script as a library, this is why I never experience this puzzling behavior before. But now it is clear, thanks.
More generally, you should arrange your imports so that there are no
cycles.  If module/script "a" imports "b", directly or indirectly, "b"
should not try to import "a."  Move the common code to a third place,
and import it from both "a" and from "b".

The other symptoms you can get are more subtle than this one, but just
as surprising.


-- 

DaveA




More information about the Python-list mailing list