surprising behaviour of global dictionaries

Peter Otten __peter__ at web.de
Tue Oct 9 13:45:04 EDT 2012


Grant Edwards wrote:

> On 2012-10-09, Peter Otten <__peter__ at web.de> wrote:
> 
>> Welcome to python -- this is a trap every newbie falls into ;)
>>
>> Seriously, you shouldn't use the main script as a library;
> 
> There must be something wrong with me.  It never even occurred to me
> to try to import a file from within that same file.  

It is typically done in two steps: 

(1) Write module x, use it in module y.
(2) For convenience add "if __name__ == '__main__'" and import module y.

Hilarity ensues.

> I don't think I've ever even heard of that before...

As I was poking fun at Michele who really is an expert and likely knows more 
about Python than I do I may have exaggerated a bit ;)

But it does come up, see

"Singleton implementation problems"
http://mail.python.org/pipermail/python-list/2008-July/470770.html

"Dynamically declared shared constant/variable imported twice problem"
http://mail.python.org/pipermail/python-list/2009-May/535619.html

There are more, but I don't know a convenient way to find the posts.
Related problems with reload() or paths into a package occur even more 
frequently.




More information about the Python-list mailing list