Globals or objects?

Hrvoje Niksic hniksic at xemacs.org
Thu Feb 21 08:01:52 EST 2008


Steve Holden <steve at holdenweb.com> writes:

> If a function uses a global variable then you have to initialize the
> same global variable in another program that uses it: yet another
> piece of setup you will forget to do.

If the global variable belongs to the module, then it is up to the
module to initialize it.  A different program program will simply
import the same module and automatically get the same variable,
properly initialized.

One reason why a singleton's attribute may be better than a global
variable is that it's easier to later add a callback when the value is
changed without breaking the interface.  A module cannot react to
module.somevar = value; on the other hand, a singleton can implement
__setattr__ that reacts to module.singleton.attr = value.



More information about the Python-list mailing list