static object

Thomas Ploch Thomas.Ploch at gmx.net
Wed Jan 3 18:02:40 EST 2007


meelab schrieb:
> Dear All,
> 
> I am looking for a way to create a "static object" or a "static class" -
> terms might be inappropriate - having for instance:
> 
> class StaticClass:
>     .
>     .
> 
> and then
> staticObject1 = StaticClass()
> staticObject2 = StaticClass()
> 
> so that staticObject1 and staticObject2 refers exactly to the same
> instance of object.
> 
> 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.
> 
> 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.
> 
> I noticed the staticmethods, and the __new__ method which could , but I
> always get stuck in actually creating static DATA without having global
> data.
> 
> Does anyone have a start of a clue to this ?
> 
> Many thanks in advance
> 
> Emmanuel.

class DataStorage:
	def __init__(self, data):
		self.data = data

dataVault = DataStorage(data)
dataVault1 = dataVault
dataVault2 = dataVault
...


but why not use a static_data.py (put your data in there) file and do:

 >>> from static_data.py import DATA

This way you only load it once and it will be accessible throughout your
program.

Thomas



More information about the Python-list mailing list