Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31051

Re: surprising behaviour of global dictionaries

From Peter Otten <__peter__@web.de>
Subject Re: surprising behaviour of global dictionaries
Date 2012-10-09 19:45 +0200
Organization None
References <40ac555b-71c2-445e-814b-6bd1d037dbee@googlegroups.com> <mailman.2006.1349796256.27098.python-list@python.org> <k51h9b$pla$1@reader1.panix.com>
Newsgroups comp.lang.python
Message-ID <mailman.2013.1349804700.27098.python-list@python.org> (permalink)

Show all headers | View raw


Grant Edwards wrote:

> On 2012-10-09, Peter Otten <__peter__@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.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

surprising behaviour of global dictionaries Michele Simionato <michele.simionato@gmail.com> - 2012-10-09 08:08 -0700
  Re: surprising behaviour of global dictionaries Peter Otten <__peter__@web.de> - 2012-10-09 17:24 +0200
    Re: surprising behaviour of global dictionaries Michele Simionato <michele.simionato@gmail.com> - 2012-10-09 08:36 -0700
      Re: surprising behaviour of global dictionaries Dave Angel <d@davea.name> - 2012-10-09 12:10 -0400
    Re: surprising behaviour of global dictionaries Michele Simionato <michele.simionato@gmail.com> - 2012-10-09 08:36 -0700
    Re: surprising behaviour of global dictionaries Grant Edwards <invalid@invalid.invalid> - 2012-10-09 15:53 +0000
      Re: surprising behaviour of global dictionaries Peter Otten <__peter__@web.de> - 2012-10-09 19:45 +0200

csiph-web