Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'importing': 0.04; 'attributes': 0.07; 'class,': 0.07; 'cursor': 0.09; 'mutable': 0.09; 'namespace': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:modules': 0.09; 'terry': 0.09; 'to)': 0.09; 'elsewhere.': 0.16; 'modules,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'sqlite3': 0.16; 'subject:class': 0.16; 'subject:instance': 0.16; 'wrote:': 0.17; 'instance': 0.17; 'instance,': 0.17; 'refers': 0.17; 'jan': 0.18; 'obviously': 0.18; 'module': 0.19; 'holds': 0.20; 'import': 0.21; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '(which': 0.26; '(most': 0.27; 'module.': 0.27; "doesn't": 0.28; 'header:X-Complaints-To:1': 0.28; '(possibly': 0.29; 'class': 0.29; 'connection': 0.30; 'attach': 0.30; 'code': 0.31; 'to:addr :python-list': 0.33; 'skip:d 20': 0.34; 'moved': 0.35; 'pm,': 0.35; 'subject:?': 0.35; 'received:org': 0.36; 'skip:u 20': 0.36; 'but': 0.36; 'modules': 0.36; 'itself': 0.37; 'does': 0.37; 'two': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'application': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'matter': 0.61; 'share': 0.61; 'first': 0.61; 'different': 0.63; 'lives': 0.71; 'received:fios.verizon.net': 0.84; 'regard,': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: best way to share an instance of a class among modules? Date: Wed, 06 Feb 2013 00:04:05 -0500 References: <15fe7153-a77a-49eb-9cf8-d4e59b4a2079@z9g2000vbx.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Thunderbird/17.0 In-Reply-To: <15fe7153-a77a-49eb-9cf8-d4e59b4a2079@z9g2000vbx.googlegroups.com> 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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360127082 news.xs4all.nl 6936 [2001:888:2000:d::a6]:57352 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38254 On 2/5/2013 11:40 PM, CM wrote: > I have recently moved all my SQLite3 database-related functions into a > class, DatabaseAccess, that lives in a "utilities" module. When the > application loads, the namespace of the instance of the class is > populated with two different cursors for two different databases the > whole application needs to use. One of those cursors always refers to > a connection to the same database, but the other cursor can change > which database it refers to; in this regard, the namespace of the > DatabaseAccess class instance holds a "state" (which is which database > the one cursor currently refers to) > > I have a number of modules that all need to use those two cursors. > However, if I import the utilities module and create a new instance of > the DatabasesAccess() in each of all the various modules, obviously > each new instance doesn't have the same namespace as the first > instance, and so doesn't have the *same* two cursors. > > I can think of a few ways to pass the DatabaseAcess instance around to > the various modules, but all of them seem like repeating myself and > clunky. What's the "best" (most Pythonic, simplest) way for a number > of modules to all share the same instance of my DatabaseAccess class? Attach the instance of the class to the utilities module that clients import. It does not matter if the code is in the utilities module itself or in one of the importing modules (possibly the first). # in module shared_cursor = # in importer import utilities utilities.shared_cursor = DatabaseAccess(args) Module attributes are mutable and can be set from elsewhere. -- Terry Jan Reedy