[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

RE: Globals or objects? (is: module as singleton

James Newton

2/21/2008 4:27:00 PM

Mel wrote:
>> James Newton wrote:
>> Could you give a bare-bones demonstration of [implementing a
singleton
>> by using a module]?
>
> I had a club-membership application that ran for several years.
> Default pathnames, etc. for the particular year came from a module
> called thisyear.py:
> #=========================
> '''Values used to access this years trakkers files.
> $Id: thisyear.py,v 1.2 2006/08/26 16:30:23 mwilson Exp $
> '''
>
> memberpath = '2006-7/20062007.txt' # path to this years membership CSV
> dirname = '2006-7' # directory name for this year
> #=========================
> Programs that needed to use the comma-separated-value membership base
> would import thisyear, and pass thisyear.memberpath when creating the
> CSV reader object. Etc.

Hi Mel,

So you were using thisyear.py as a preferences file: making it a module
was a shortcut for reading in the file and parsing its contents. I like
it.

Would there be any circumstances where the singleton module would
include functions and objects? In particular, would this system work if
the application needed to change a value (such as a counter)?

Suppose your application wanted to save the changed value, so that the
next session started using the new value. Could you simply write out a
new copy of the thisyear.py file? Or would this lead to version issues?
Would a previously-imported version of the module be stored in a
different place in RAM than a module that was imported after the change
was made?

Perhaps my real question is about how to visualize a module: what makes
an imported module different from an instance?

Thanks for your insights,

James
1 Answer

Duncan Booth

2/21/2008 4:41:00 PM

0

"James Newton" <jnewton@fuelindustries.com> wrote:

> Perhaps my real question is about how to visualize a module: what makes
> an imported module different from an instance?

On one level: nothing. An imported module is an instance of the module
type. Modules don't have to be associated with python code: you can create
additional module instances (by calling new.module) and populate them
however you wish.

What you get with a module is support for locating a specific module and
ensuring that you don't get duplicate copies of a named module.

What you lose is the ability to define methods: functions in a module don't
have a 'self' parameter. Also you can't override special methods for a
module, so it isn't quite equivalent to writing a class, but there are
similarities.

Regarding your question about saving the values: what you would usually do
would be to store the values in a separate configuration file and the
module would load them on startup and then rewrite the configuration file
when you call a save function. That way the module code itself isn't
changing. The 'singleton' config module would expose functions such as
'load', 'save', 'getXXX'.