By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,198 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

static object

P: n/a
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:
Jan 3 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
That looks like some kind of singleton. Why don't you use a module
instead of a class?

Another solution is to define your data as class attributes.

Bye,
bearophile

Jan 3 '07 #2

P: n/a
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
Jan 3 '07 #3

P: n/a
In article <45*********************@news.free.fr>,
meelab <me****@free.frwrote:
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.
Personally I do the following (in its own module). There may be a better
way, but this is simple and it works:

_theSingleton = None

def getSingleton():
global _theSingleton
if not _theSingleton:
_theSingleton = _Singleton()
return _theSingleton

class _Singleton:
def __init__(self, ...):
...
-- Russell
Jan 3 '07 #4

P: n/a
At Wednesday 3/1/2007 19:38, meelab wrote:
>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.
This is usually known as a Singleton.
If you only want it to store values, or use "normal" methods, the
most direct way is a simple module - modules are already singletons in Python.
If you want to include properties, descriptors, and such, look for
the Singleton (or Borg) pattern in the Python Cookbook:
http://aspn.activestate.com/ASPN/Cookbook/Python/
--
Gabriel Genellina
Softlab SRL


__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Jan 4 '07 #5

P: n/a
On 1/3/07, meelab <me****@free.frwrote:
I am looking for a way to create a "static object" or a "static class" -
terms might be inappropriate - having for instance:
An example will speak better than me:

class Card(object):
__cards = {}

def __init__(self, number, suit):
self.number, self.suit = number, suit

def __new__(cls, number, suit):
try:
return cls.__cards[(number, suit)]
except KeyError:
obj = object.__new__(cls, number, suit)
cls.__cartas[(number, suit)] = obj
return obj
--
Felipe.
Jan 4 '07 #6

P: n/a
meelab <me****@free.frwrites:
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

Jan 4 '07 #7

P: n/a
Ben Finney a écrit :
(snip)
The "Singleton" pattern does what you say here. Implementing a proper
Singleton in Python is complicated and hard to understand.
Really ? Using __new__ and a class attribute, it doesn't seem so
complicated - nor difficult to understand...
Jan 4 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.