473,320 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Re: Singleton implementation problems

On Jul 3, 6:58 pm, Urizev <uri...@gmail.comwrote:
Hi everyone

I have developed the singleton implementation. However I have found a
strange behaviour when using from different files. The code is
attached.

Executing main
new MySet object
No singleton instance
New singleton:
<__main__.Singleton instance at 0x2b98be474a70>
new MySet object
There is a singlenton instance
new Member
new MySet object
No singleton instance
New singleton:
<myset.Singleton instance at 0x2b98be474d88>
new Member
new MySet object
There is a singlenton instance
new Member
new MySet object
There is a singlenton instance

I do not know why, but it creates two instances of the singleton. Does
anybody know why?
Because __init__() is called to initialize the state of an object
*after* it has already been created. You should create a "new-style"
class and define __new__() instead. Here's a working version:

class Singleton(object): # new-style class
def __new__(cls, *args, **kwds):
# return the singleton (if already created)
try: return cls.__dict__['__singleton']
except KeyError: # raised only the first time for a given
class
# create the singleton and store it to the class namespace
singleton = object.__new__(cls, *args, **kwds)
setattr(cls, '__singleton', singleton)
return singleton

class X(Singleton):
def __init__(self, a): self.a = a

assert X(1) is X(2)
Note however that the classic Singleton pattern is usually frowned
upon in Python; the preferred approach is to use (module level)
globals. Also search for the "Borg pattern".

George
Jul 4 '08 #1
0 1323

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: cppaddict | last post by:
Hi, In this tutorial on singleton class in C++ (http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp) the author gives two implementations of a simple singleton class, claiming that...
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
10
by: ferdinand.stefanus | last post by:
Hi Could someone tell me what's the difference between these two singleton implementations: First implementation: class Singleton { public:
5
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
9
by: VidalSasoon | last post by:
I am trying to set up a singleton since it seems like the best solution to my problem. I have a an object that I want to initialize when the program starts then I just want to call it like a...
2
by: Gabriele Farina | last post by:
Hi, I'm tring to implement a Singleton object that should be specific for every thread who create it, not global. I tried a solution that seems to work, but I have a very poor knowledge of...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
3
by: koredump | last post by:
I have inherited a Business Object architecture that makes heavy use of the Singleton design patter. For example the data access layer class is implemented as a static Singleton "object", there...
3
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design...
0
by: Matthew Fitzgibbons | last post by:
Urizev wrote: I've run into a similar problem. Basically, when you refer to a class from __main__ (i.e., the .py file you're actually running) versus from an imported module, Python doesn't...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.