Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'see.': 0.07; 'cursor': 0.09; 'imported': 0.09; 'method:': 0.09; 'namespace': 0.09; 'subject:modules': 0.09; 'def': 0.10; '"global"': 0.16; 'class)': 0.16; 'cleaner': 0.16; 'created.': 0.16; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; "method's": 0.16; "module's": 0.16; 'namespace,': 0.16; 'namespace.': 0.16; 'namespace?': 0.16; 'subject:class': 0.16; 'subject:instance': 0.16; 'wrote:': 0.17; 'module': 0.19; 'variable': 0.20; 'trying': 0.21; 'stick': 0.22; 'runs': 0.22; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '----': 0.27; 'class': 0.29; 'keyword': 0.30; 'function': 0.30; 'code': 0.31; 'to:addr:python- list': 0.33; 'another': 0.33; 'pm,': 0.35; 'subject:?': 0.35; 'something': 0.35; 'received:org': 0.36; 'but': 0.36; 'message- id:@gmail.com': 0.36; 'level.': 0.36; 'method': 0.36; 'does': 0.37; 'two': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'your': 0.60; "you've": 0.61; 'first': 0.61; 'skip:n 10': 0.63; 'within': 0.64 X-Virus-Scanned: amavisd-new at torriefamily.org Date: Wed, 06 Feb 2013 18:08:49 -0700 From: Michael Torrie User-Agent: Mozilla/5.0 (X11; Linux i686; rv:10.0.12) Gecko/20130105 Thunderbird/10.0.12 MIME-Version: 1.0 To: python-list@python.org Subject: Re: best way to share an instance of a class among modules? References: <15fe7153-a77a-49eb-9cf8-d4e59b4a2079@z9g2000vbx.googlegroups.com> <5db4add7-ab9c-4311-b3b6-24156cd6f248@z4g2000vbz.googlegroups.com> <5112EF48.6040404@gmail.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 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: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360199338 news.xs4all.nl 6920 [2001:888:2000:d::a6]:43185 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38322 On 02/06/2013 05:45 PM, CM wrote: > But the function in the module is also within a *class* so I don't > think the function does have access to the module's global namespace. > Here's the hierarchy: > > -- Module namespace.... > ---- class namespace (DatabaseAccess is the name of the class) > ---- function namespace > This is where the cursor is created. How do I get it > into the module namespace? Oh I see. You're trying to set a variable in the current module's global namespace. There's two ways to do this, the first one is cleaner and preferred: 1. Have a function in DatabaseAccess return the cursor then then set it to a name in code that runs at the module level. 2. use the "global" keyword in the method: def mymethod(self): global blah blah = something Then your module has has blah in it's global method now. If you want to stick a variable in another method's global namespace, as long as you've imported it you just go: module.newvariable = something