Globals or objects? (is: module as singleton)

Duncan Booth duncan.booth at invalid.invalid
Fri Feb 22 05:42:36 EST 2008


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

> Duncan Booth wrote: 
>> you can create additional module instances (by calling new.module)
> 
> Hi Duncan,
> 
> Could you provide a scenario where this would be useful (and the best
> practice)?

Not really as such cases are few and far between. Try grepping the standard 
library for a few examples where new.module is called.

>> 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.
> 
> So if I were to execute the following pseudo-code, the second 'import'
> would simply point at the module (instance) imported the first time:
> 
> import mymodule
> changeContentsOf("mymodule.py") #on the hard disk
> import mymodule
> 
> The values, functions and classes available in mymodule would only
> change if I were to restart the application.

Yes, you could call 'reload(mymodule)' to force it to reload but otherwise 
once it has been imported once any subsequent imports just return the 
existing module.




More information about the Python-list mailing list