Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'newbie': 0.05; 'paths': 0.05; 'url:pipermail': 0.05; '__name__': 0.07; 'python': 0.09; '"if': 0.09; 'convenience': 0.09; 'imported': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'subject:dictionaries': 0.16; 'trap': 0.16; 'wrote:': 0.17; "shouldn't": 0.17; 'module': 0.19; 'file.': 0.20; 'bit': 0.21; 'import': 0.21; "i've": 0.23; 'script': 0.24; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.28; 'declared': 0.29; 'falls': 0.29; 'occurred': 0.29; 'steps:': 0.29; 'related': 0.30; 'knows': 0.30; 'url:python': 0.32; 'file': 0.32; '(2)': 0.32; 'more,': 0.32; 'to:addr:python-list': 0.33; 'likely': 0.33; 'typically': 0.33; '(1)': 0.34; 'wrong': 0.34; 'done': 0.34; 'something': 0.35; 'there': 0.35; 'add': 0.36; 'received:org': 0.36; 'really': 0.36; 'but': 0.36; 'url:org': 0.36; 'problems': 0.36; 'does': 0.37; 'two': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'url:mail': 0.40; 'think': 0.40; 'expert': 0.62; 'ever': 0.63; 'more': 0.63; 'within': 0.64; 'fun': 0.64; 'before...': 0.84; 'otten': 0.84; 'edwards': 0.91; 'seriously,': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: surprising behaviour of global dictionaries Date: Tue, 09 Oct 2012 19:45:04 +0200 Organization: None References: <40ac555b-71c2-445e-814b-6bd1d037dbee@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084a243.dip.t-dialin.net User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1349804700 news.xs4all.nl 6924 [2001:888:2000:d::a6]:54927 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:31051 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.