singleton decorator

A

Andre Meyer

While looking for an elegant implementation of the singleton design pattern
I came across the decorator as described in
PEP318<http://www.python.org/dev/peps/pep-0318/>
.
Unfortunately, the following does not work, because decorators only work on
functions or methods, but not on classes.

def singleton(cls):
instances = {}
def getinstance():
if cls not in instances:
instances[cls] = cls()
return instances[cls]
return getinstance

@singleton
class MyClass:
...


Am I missing something here? What is the preferred pythonic way of
implementing singleton elegantly?

Thanks for your help
André
 
F

Farshid Lashkari

Andre said:
Am I missing something here? What is the preferred pythonic way of
implementing singleton elegantly?

The "Open Issues" section of that PEP says the following:

"It's exceedingly unlikely that class decorators will be in Python 2.4"

So it might not be in the current version of python.

-Farshid
 
E

Erik Max Francis

Andre said:
Am I missing something here? What is the preferred pythonic way of
implementing singleton elegantly?

Create a class and then derive from it. There are examples on the Cookbook.
 
C

Chaz Ginger

Andre Meyer:

Maybe to just use a module.

Bye,
bearophile
Here is some sample code for both singleton classes and named classes
that I use:
class Singleton(type):
"""
This class will allow only one instance of a type
regardless of the initialization arguments.
"""
def __init__(self, *args, **kwargs):
"""
This is done when the class is defined.
"""
type.__init__(self, *args, **kwargs)
self._instance = None

def __call__(self, *args, **kwargs):
"""
This is called when the class is instantiated.
"""
if not self._instance : self._instance = type.__call__(self,*args,**kwargs)
return self._instance

class namedSingleton(type):
"""
This class will allow a different singleton per initialization
argument list. This implementation does not take into account
variations in the keyword args.
"""
def __init__(self, *args, **kwargs):
"""
This is executed when the class is first defined.
"""
type.__init__(self, *args, **kwargs)
self._instances = {}

def __call__(self, *args, **kwargs):
"""
This is called when the class is instantiated.
"""
if not args in self._instances:
self._instances[args] = type.__call__(self, *args,**kwargs )
return self._instances[args]
 
G

Georg Brandl

Andre said:
While looking for an elegant implementation of the singleton design
pattern I came across the decorator as described in PEP318
<http://www.python.org/dev/peps/pep-0318/>.
Unfortunately, the following does not work, because decorators only work
on functions or methods, but not on classes.

def singleton(cls):
instances = {}
def getinstance():
if cls not in instances:
instances[cls] = cls()

return instances[cls]
return getinstance

@singleton
class MyClass:
...


Am I missing something here? What is the preferred pythonic way of
implementing singleton elegantly?

You can always use the syntax the decorator replaces:

class MyClass:
...
MyClass = singleton(MyClass)

But there are better ways, and maybe you don't even need a singleton.
See the Python Cookbook.

Georg
 
P

Paddy

Andre said:
Am I missing something here? What is the preferred pythonic way of
implementing singleton elegantly?

Thanks for your help
André

Hi Andre,
You might also google for
python borg pattern
As a discussion on the 'borg' design pattern might be informative.

- Pad.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,592
Members
45,104
Latest member
LesliVqm09
Top