Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38254
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: best way to share an instance of a class among modules? |
| Date | 2013-02-06 00:04 -0500 |
| References | <15fe7153-a77a-49eb-9cf8-d4e59b4a2079@z9g2000vbx.googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1401.1360127082.2939.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
best way to share an instance of a class among modules? CM <cmpython@gmail.com> - 2013-02-05 20:40 -0800
Re: best way to share an instance of a class among modules? Terry Reedy <tjreedy@udel.edu> - 2013-02-06 00:04 -0500
Re: best way to share an instance of a class among modules? CM <cmpython@gmail.com> - 2013-02-06 14:41 -0800
Re: best way to share an instance of a class among modules? Michael Torrie <torriem@gmail.com> - 2013-02-06 17:03 -0700
Re: best way to share an instance of a class among modules? c <chaelon@gmail.com> - 2013-02-06 16:43 -0800
Re: best way to share an instance of a class among modules? Ethan Furman <ethan@stoneleaf.us> - 2013-02-06 16:57 -0800
Re: best way to share an instance of a class among modules? Ethan Furman <ethan@stoneleaf.us> - 2013-02-06 17:36 -0800
Re: best way to share an instance of a class among modules? Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-07 18:14 -0800
Re: best way to share an instance of a class among modules? Michael Torrie <torriem@gmail.com> - 2013-02-07 23:05 -0700
Re: best way to share an instance of a class among modules? Michael Torrie <torriem@gmail.com> - 2013-02-08 21:54 -0700
Re: best way to share an instance of a class among modules? Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-07 18:14 -0800
Re: best way to share an instance of a class among modules? Michael Torrie <torriem@gmail.com> - 2013-02-06 17:14 -0700
Re: best way to share an instance of a class among modules? CM <cmpython@gmail.com> - 2013-02-06 16:45 -0800
Re: best way to share an instance of a class among modules? Michael Torrie <torriem@gmail.com> - 2013-02-06 18:08 -0700
csiph-web