Globals or objects? (is: module as singleton)

Duncan Booth duncan.booth at invalid.invalid
Thu Feb 21 11:40:42 EST 2008


"James Newton" <jnewton at 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'.




More information about the Python-list mailing list