Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!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.027 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; '(at': 0.04; 'class,': 0.07; 'patterns.': 0.07; 'wrong,': 0.09; "wouldn't": 0.14; '#ifdef': 0.16; 'behavior:': 0.16; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; 'simple.': 0.16; 'subject:between': 0.16; 'subject:object': 0.16; 'wrote:': 0.18; 'module': 0.19; 'basically': 0.19; 'import': 0.22; 'header:User- Agent:1': 0.23; 'module,': 0.24; 'tells': 0.24; 'header': 0.24; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'generally': 0.29; 'especially': 0.30; "i'm": 0.30; 'included': 0.31; 'code': 0.31; 'though.': 0.31; 'up.': 0.33; 'plain': 0.33; 'common': 0.35; 'something': 0.35; 'but': 0.35; 'really': 0.36; 'module.': 0.36; 'processed': 0.36; 'too': 0.37; 'message-id:@gmail.com': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'pm,': 0.38; 'expect': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'even': 0.60; 'up,': 0.60; 'matter': 0.61; 'course': 0.61; 'simple': 0.61; "you're": 0.61; 'first': 0.61; 'times': 0.62; 'email addr:gmail.com': 0.63; 'charset:windows-1252': 0.65; 'hang': 0.67; 'behavior': 0.77; 'subject:Best': 0.91 X-Virus-Scanned: amavisd-new at torriefamily.org Date: Mon, 23 Feb 2015 14:10:07 -0700 From: Michael Torrie User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Best practice: Sharing object between different objects References: In-Reply-To: Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit 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: 19 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424725814 news.xs4all.nl 2862 [2001:888:2000:d::a6]:35270 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86270 On 02/23/2015 01:02 PM, sohcahtoa82@gmail.com wrote: > That behavior always trips me up. My intuition tells me that every > time you import a module, it re-runs the code in the module. So if I > had a simple module named myModule.py that had a single line with > `myInt = 1`, then I would *EXPECT* this behavior: Basically your intuition is wrong, but I'm not really sure why you think this in the first place, especially if you're familiar with design patterns. A module is a singleton. Plain and simple. If you expect a module to reinstantiate something every time you import it, then what you want is a class, not a module. Even if you think of modules like C header files, as far as use goes, you wouldn't be too far wrong. Generally speaking header files are processed only once no matter how many times they are included (at least the common ones with #ifdef fences in them). I can see how coming from PHP would trip you up, though. Of course I never got the hang of include vs include_once vs require vs require_once.