473,513 Members | 2,429 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static object

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
7 1352
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3798
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
2603
by: nytimescnn | last post by:
I've read some discuession about lock() for thread-safe. I am wondering what will be the differce between below two code segment? Code 1: class A { private static Object padlock = new...
14
2552
by: Jeroen | last post by:
Hi all, I've got a question about writing a library. Let me characterize that library by the following: * there is a class A which is available to the user * there is a class B that is used...
1
3510
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
14
5980
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
0
7388
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7545
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7539
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5692
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5095
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3240
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
461
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.