static object

Ben Finney bignose+hates-spam at benfinney.id.au
Wed Jan 3 20:28:55 EST 2007


meelab <meelab at free.fr> writes:

> In other words, that is a class which would result in only 1 instance
> always the same no matter how many times I will "instantiate" it.

The "Singleton" pattern does what you say here. Implementing a proper
Singleton in Python is complicated and hard to understand.

However, there's nothing in your description that requires only one
*instance* of the class; you only require that any instance of that
class should share the same *state* with all other instances.

If that's true, you'll be better served by the much simpler Borg class
(referred to elsewhere in this thread, but for reference
<URL:http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531>).

> My purpose is to permit this class to initialize a massive amount of
> data that I need to access from different points of my program
> without duplicating this data in memory and without loosing time in
> reloading it each time I need it.

This can be further simplified by instantiating all that stuff in a
*module*, and importing the module wherever it is needed. No need for
custom classes at all, then.

-- 
 \       "Even if the voices in my head are not real, they have pretty |
  `\                                        good ideas."  -- Anonymous |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list